Socket 编程—多个客户端
程序员文章站
2022-04-03 20:49:47
...
在Socket编程 - 单个客户端中,服务端只支持单个客户端的连接请求。
如果有多个客户端连接呢?需要有一个线程一直监听客户端请求,收到请求后分发给其它的线程处理。这和jetty的处理方式类似,只需要改动服务端的代码就可以了。
服务端:
package com.bijian.study02; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class MyMultiClientsServer { private static final int SERVER_PORT = 4451; ServerSocket server; Socket socket; public MyMultiClientsServer() { try { server = new ServerSocket(SERVER_PORT); while (true) { socket = server.accept(); new DispatchRequestThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } finally { // Release the resources opened } } class DispatchRequestThread extends Thread { BufferedReader in; PrintWriter out; Socket socket; public DispatchRequestThread(Socket socket) { this.socket = socket; } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); while (true) { String str = in.readLine(); System.out.println(socket.getPort() + ": " + str); out.println("Received: " + str); if (str.equals("end")) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { // Release the resources opened } } } public static void main(String[] args) { new MyMultiClientsServer(); } }
客户端:(完全同Socket 编程—单个客户端中的客户端)
package com.bijian.study02; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class MyClient { Socket connection; BufferedReader in; PrintWriter out; BufferedReader reader; public MyClient() { try { // Establish a connection to the Socket server connection = new Socket(InetAddress.getLocalHost(), 4451); // Construct a reader to get the response from the server in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // Construct a writer to send messages to the server out = new PrintWriter(connection.getOutputStream()); // Construct a reader to accept the user input reader = new BufferedReader(new InputStreamReader(System.in)); // Continuously send out the messages to the server and receive the response while (true) { String str = reader.readLine(); out.println(str); out.flush(); System.out.println("sent out: " + str); if (str.equals("end")) { break; } System.out.println(in.readLine()); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // Release the resources - omitted } } public static void main(String[] args) { new MyClient(); } }
开启服务端后,开启多个客户端进行测试,结果如下:
客户端1:
hello sent out: hello Received: hello boy sent out: boy Received: boy bye sent out: bye Received: bye end sent out: end
客户端2:
hello sent out: hello Received: hello girl sent out: girl Received: girl bye sent out: bye Received: bye end sent out: end
服务端:
51373: hello 51373: boy 51377: hello 51377: girl 51373: bye 51377: bye 51377: end 51373: end