server.java import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { private int port = 8006; private ServerSocket serverSocket; public Server() throws IOException{ serverSocket = new ServerSocket(port,3); System.out.println("服务器已启动!!!"); } public void service(){ while(true){ Socket socket = null; try{ socket = serverSocket.accept(); System.out.println("服务器新建立连接"+socket.getInetAddress()+":"+socket.getPort()); }catch(IOException e){ e.printStackTrace(); }finally{ try{ if(socket!=null) socket.close(); }catch(IOException e){ e.printStackTrace(); } } } } public static void main(String args[])throws Exception{ Server server = new Server(); //Thread.sleep(60000*10); server.service(); } } client.java import java.net.Socket; public class Client { public static void main(String[] args)throws Exception{ final int length=100; String host = "localhost"; int port =8006; Socket[] sockets = new Socket[length]; for(int i=1;i<length;i++){ sockets[i]= new Socket(host,port); System.out.println("第"+i+"次链接成功"); } Thread.sleep(3000); for(int i=0;i<length;i++){ sockets[i].close(); } } }