请帮助我,我无法读取或写入服务器客户端,我不知道为什么?

时间:2018-02-16 19:34:17

标签: java

请帮助我,我无法读取或写入服务器客户端,我不知道为什么?

服务器应用

public static void main(String[] args)throws Exception {
    ServerSocket ss = new ServerSocket(9000);

    while(true){
        Socket s = ss.accept();
        System.out.println("connected to client "+s.getInetAddress().getHostName());

        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream(),true);

        BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

        String msgin;
        while((msgin=in.readLine())!=null){
            System.out.println(msgin);
        }

        String msgout  = stdin.readLine();
        out.println("Server: "+msgout);

    }
}

客户端应用

public class Client {

    public static void main(String[] args)throws Exception {
            Socket s =new Socket("localhost",9000);

            while(true){
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter out = new PrintWriter(s.getOutputStream(),true);

            BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

            String msgin;
            while((msgin=in.readLine())!=null){
                System.out.println(msgin);
            }

            String msgout  = stdin.readLine();
            out.println("Client: "+msgout);
            }
    }

}

1 个答案:

答案 0 :(得分:2)

双方从Socket读取数据直到它关闭。这种情况并没有发生,所以双方都会等待来自另一方的数据,这是典型的僵局。