实例一:作为服务端,使用线程池接收多个客户端的TCP请求
String port=9015; ServerSocket serverSocket =new ServerSocket(port); Executor exe=Executors.newCachedThreadPool(); while(true) { final Socket socket=serverSocket.accept(); exe.execute(new Runnable(){ @Override public void run(){ InputStream in=null; OutputStream out=null; PrintWriter printWriter=null; try{ in=socket.getInputStream(); byte[] buffer=new byte[2048]; while(true){ int len=in.read(buffer); String packetContent=new String(buffer,0,len); } } } }); }