我的客户端无法读取服务器的响应

时间:2014-02-03 08:08:28

标签: java client-server

这是我服务器的代码

    String request=di.readLine();
    System.out.print(request);

    switch (request){
        case "/login":                
         dout.writeBytes(request);
    }

这是给我的客户

String command = "/login \r\n";
       try{
       Socket s = new Socket ("localhost",8000);
       DataInputStream di = new DataInputStream(s.getInputStream());
       DataOutputStream dout = new DataOutputStream(s.getOutputStream());


       dout.writeBytes(command);
       System.out.println(di.readLine());

       }catch(Exception e){
           System.out.println("Log in error");
           e.printStackTrace();
       }

服务器可以获取我的命令但我的客户端无法读取服务器的响应 我已经在我的telnet中尝试了它并且它有效。我认为问题出在我的客户端程序代码中

2 个答案:

答案 0 :(得分:0)

readLine()删除了行终止符,所以当你将结果发送回对等体的readLine()时,没有行终止符,所以对等体的readLine()会一直等待它。

答案 1 :(得分:-1)

不推荐使用DataInputStream的readLine方法 - 不要使用它。

将其替换为:

BufferedReader br = new BufferedReader(new InputStreamReader(in));

如果要发送内容,请拨打.flush()。输出流是缓冲的,只有在缓冲区已满或调用flush()或流关闭时才会发送数据。