java Socket通信编程
客户端:Client.java
package learn;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;/** * @version 1.0 * @date 2012-2-12 下午02:20:12 * @author lixunhui * @since JDK1.6 */ public class Server {public static void main(String[] args) throws IOException {ServerSocket server = null;Socket client=null;BufferedReader read=null;PrintWriter write=null;server=new ServerSocket(9999);boolean flag=true;while(flag){System.out.println("服务器运行,等待客户端连接......");client=server.accept();read=new BufferedReader(new InputStreamReader(client.getInputStream()));write=new PrintWriter(client.getOutputStream(),true);boolean f=true;while(f){String str=read.readLine();System.out.println(str);if(str==null||"".equals(str)){f=false;}else{if(str.equalsIgnoreCase("bye")){f=false;}else{write.println("ECHO:"+str);}}}}}}
?
?
?