好学IT学院:IT信息技术分享交流平台
标签:Java  来源:www.hxw.red  作者:本站整理  发布时间:2009-07-31  ★★★加入收藏〗〖手机版
摘要:关于java代码与编程的综合应用135、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?答:父类:package test;public class FatherClass public FatherClass() Syste……

138、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?

答:Server端程序:

package test;
import java.net.*;
import java.io.*;

public class Server{
  private ServerSocket ss;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  public Server() {
  try {
   ss=new ServerSocket(10000);
   while(true) {
  socket = ss.accept();
  String RemoteIP = socket.getInetAddress().getHostAddress();
  String RemotePort = ":"+socket.getLocalPort();
  System.out.println("A client come in!IP:"+RemoteIP+RemotePort);
  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  String line = in.readLine();
  System.out.println("Cleint send is :" + line);
  out = new PrintWriter(socket.getOutputStream(),true);
  out.println("Your Message Received!");
  out.close();
  in.close();
  socket.close();
   }
  }catch (IOException e) {
   out.println("wrong");
  }
 }
 public static void main(String[] args) {
  new Server();
 }
}

Client端程序:

package test;
import java.io.*;
import java.net.*;

public class Client {
 Socket socket;
 BufferedReader in;
 PrintWriter out;
 public Client() {
  try {
   System.out.println("Try to Connect to 127.0.0.1:10000");
   socket = new Socket("127.0.0.1",10000);
   System.out.println("The Server Connected!");
   System.out.println("Please enter some Character:");
   BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
   out = new PrintWriter(socket.getOutputStream(),true);
   out.println(line.readLine());
   in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   System.out.println(in.readLine());
   out.close();
   in.close();
   socket.close();
  }catch(IOException e) {
   out.println("Wrong");
  }
 }
 public static void main(String[] args) {
  new Client();
 }
}

用Java实现音频播放
  桌面PC 的性能日益提高,Java虚拟机的优化技术也不断获得突破,这一切使得用Java处理实时信号成为可能。本文将通过设计和构造一…
  • 好学触屏公众号虎力全开、杨帆起航!
  • 好学考试H5触屏版开放内测