客户端和服务器启动后可以持续通信(自由交谈)直到客户端输入“bye”结束。(可否启动多个客户端同时与服务器通信?)
服务器代码:
1 package cn.edu.sise; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.net.InetSocketAddress; 9 import java.net.ServerSocket;10 import java.net.Socket;11 import java.net.SocketAddress;12 13 public class EchoServer {14 15 public static void main(String[] args) {16 //1.启动服务器17 ServerSocket listenSocket = null; 18 Socket clientSocket = null; 19 BufferedReader in = null;20 BufferedWriter out = null;21 try {22 listenSocket = new ServerSocket();23 SocketAddress serverAddr=new InetSocketAddress("localhost",5000);24 listenSocket.bind(serverAddr); 25 System.out.println("1.服务器启动成功!开始在localhost的5000端口侦听连接...");26 //2.处理连接 27 clientSocket = listenSocket.accept();28 System.out.println("2.客户机连接成功!客户机地址和端口:"+clientSocket.getRemoteSocketAddress());29 //3.与客户机会话 30 in = new BufferedReader(31 new InputStreamReader(32 clientSocket.getInputStream())); 33 out = new BufferedWriter(34 new OutputStreamWriter( 35 clientSocket.getOutputStream()));36 37 /*boolean shutdown = false;38 while(!shutdown){39 String recvStr = in.readLine(), sentStr = ""; //从客户机接收字符串40 System.out.println("server recv:" + recvStr);41 switch (recvStr){42 case "bye":43 sentStr = "Good Bye!";44 shutdown = true;45 break;46 default:47 sentStr = recvStr;48 } 49 out.write(sentStr); //向客户机回送字符串50 out.newLine();51 out.flush();52 }53 */54 boolean bool = false;55 while(!bool){56 String recvStr = in.readLine(), sentStr ="";//从客户机接收字符串57 System.out.println("server recv:" + recvStr);58 if(recvStr == "bye"){59 sentStr = "Good Bye!";60 bool = true;61 break;62 }63 else{64 sentStr = recvStr;65 }66 out.write(sentStr); //向客户机回送字符串67 out.newLine();68 out.flush();69 }70 } catch (IOException ex) {71 System.out.println("异常信息:"+ex.getMessage());72 }73 //4.关闭套接字和流74 try { 75 if (in != null)in.close();76 if (out != null) out.close();77 if (listenSocket != null) listenSocket.close();78 if (clientSocket != null) clientSocket.close(); 79 System.out.println("4.关闭套接字和流成功!");80 } catch (IOException ex) {81 System.out.println("异常信息"+ex.getMessage());82 }83 }84 }
客服机代码:
1 package cn.edu.sise; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.net.InetSocketAddress; 9 import java.net.Socket;10 import java.net.SocketAddress;11 12 public class EchoClient {13 14 public static void main(String[] args) {15 Socket clientSocket = null;16 BufferedReader in = null;17 BufferedWriter out = null;18 try {19 //1.创建客户机套接字20 clientSocket = new Socket();21 SocketAddress remoteAddr=new InetSocketAddress("localhost",5000); 22 System.out.println("1.创建客户机套接字成功!");23 //2.连接服务器24 clientSocket.connect(remoteAddr);25 System.out.println("2.客户机连接服务器localhost端口5000成功!");26 System.out.println("客户机使用的地址和端口:"+clientSocket.getLocalSocketAddress());27 //与服务器会话 28 in = new BufferedReader(29 new InputStreamReader(30 clientSocket.getInputStream())); 31 out = new BufferedWriter(32 new OutputStreamWriter( 33 clientSocket.getOutputStream()));34 //String sendStr="有朋自远方来";35 //String sendStr ="", recvStr;36 String recvStr;37 while(true){38 System.out.print("input message: ");39 BufferedReader localReader=new BufferedReader40 (new InputStreamReader(System.in));41 String sendStr=localReader.readLine();42 System.out.println("send: " + sendStr);43 out.write(sendStr);44 out.newLine();45 out.flush();46 recvStr = in.readLine();47 System.out.println("recv: "+ recvStr);48 if(sendStr.equals("bye")){49 break;50 }51 }52 } catch (IOException ex) {53 System.out.println("异常信息:"+ex.getMessage());54 }55 //关闭套接字和流56 try {57 if (in != null) in.close();58 if (out != null) out.close();59 if (clientSocket != null) clientSocket.close(); 60 System.out.println("4.关闭套接字和流成功!");61 } catch (IOException ex) {62 System.out.println("异常信息:"+ex.getMessage());63 } 64 }65 }
运行结果如下(客服机):
服务器结果: