public class SocketServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8888); while (true) { //阻塞方法 Socket socket = serverSocket.accept(); System.out.println("有客户端连接了。。"); new Thread(new Runnable() { @Override public void run() { try { handler(socket); } catch (IOException e) { } } }).start(); } } private static void handler(Socket socket) throws IOException { System.out.println("thread id = " + Thread.currentThread().getId()); byte[] bytes = new byte[1024]; System.out.println("准备read。。"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); // TODO 读取客户端信息并处理 } }
普通socket服务端
最新推荐文章于 2023-03-17 14:39:55 发布