ServerSocket端连接多个客户端为什么做成死循环,可以详细解释下吗?谢谢咯
public class TCPServer {
//首先启动server再启动client

public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(6666);
//多个客服端可以申请连接服务端,server端一般不间断,所以做一个死循环
while(true){
Socket s = ss.accept();
//拿到输入管道
System.out.println("A Client connect");
InputStream is = s.getInputStream();
//创建输入流把管道穿进去,用dis读数据
DataInputStream dis = new DataInputStream(is);
//readUTF阻塞式的读东西
System.out.println(dis.readUTF());
dis.close();
s.close();
}
}
}

解决方案 »

  1.   

    死循环意思就是一直在等待其他客户端的接入,不然每一次接入客户端都要开启服务器。而且还有服务器就是一直在工作的,即使没有接入客户端,也是一直在工作待命。代码最后
    dis.close();
    s.close();把当前Client和Server的连接通道关掉了,也就是释放了当前的通道资源,等待其他Client接入
      

  2.   

    1.你这个只支持单一客户端
    2.如果需要做网络编程,请跟进mina或者netty,对你的能力提升日后工作帮助更大
      

  3.   

    总的来说坑很多,使用框架吧。巩固下知识,周末写段代码练手,仅供参考,勉强运行    public static int port = 6666;    @Test
        public void server() throws IOException {
            ServerSocket ss = new ServerSocket(port);
            while (true) {
                Socket socket = ss.accept();
                new Thread(() -> {
                    try (InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream();) {
                        byte[] bs = new byte[1024];
                        while (true) {
                            int count = inputStream.read(bs);
                            if (count < 1) continue;
                            System.out.println("服务器接收信息" + new String(bs, 0, count));
                            Arrays.fill(bs, (byte) 0);
                            outputStream.write(("服务器发送信息:" + " date:" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)).getBytes());
                            outputStream.flush();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }    @Test
        public void clinets() throws InterruptedException {
            IntStream.range(1, 4).mapToObj(Client::new).forEach(client -> new Thread(client::message).start());
            TimeUnit.HOURS.sleep(1);
        }    public static class Client {
            private Socket socket;
            private int clientId;        public Client(int clientId) {
                this.clientId = clientId;
                try {
                    this.socket = new Socket("127.0.0.1", port);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }        public void message() {
                try {
                    InputStream inputStream = socket.getInputStream();
                    OutputStream outputStream = socket.getOutputStream();
                    byte[] bs = new byte[1024];
                    while (true) {
                        outputStream.write(("clientId:" + clientId + " date:" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)).getBytes());
                        outputStream.flush();
                        int count = inputStream.read(bs);
                        System.out.println(new String(bs, 0, count));
                        Arrays.fill(bs, (byte) 0);
                        TimeUnit.SECONDS.sleep(10);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }