readLine()永远不会停止阅读

时间:2013-04-04 09:04:54

标签: java readline

inFromClientR.readLine()永不停止。有任何想法吗?我忘记了什么吗?

服务器:

/*{ some code:
    send a file with a dataoutputstream to client using a new port(4000) and when transfer is done i want a responce message (e.g. OK) send back to server in the old port(6000)  
}*/
 ServerSocket listenTransferSocket = new ServerSocket(6000);
      Socket connectionTransferSocket = listenTransferSocket.accept();

    BufferedReader inFromClientR =
             new BufferedReader(new InputStreamReader(connectionTransferSocket.getInputStream()));

     System.out.println("Client's response to Transfer: " +inFromClientR.readLine());

客户端:

/*{ some code: 
receive the file on port (4000) and then the responce is sent to server using the following commands
}*/
Socket fileTransferSocket = new Socket("localhost", 6000);

 DataOutputStream outToServerR = 
       new DataOutputStream(fileTransferSocket.getOutputStream()); 

        outToServerR.writeBytes("Transfer completed " +'\n');

2 个答案:

答案 0 :(得分:1)

BufferedReader#readLine()尝试用8192字节填充其缓冲区,同时忽略它找到的任何换行符。由于您打开了连接,接收方将等到1)您已发送8192个字节,或2)关闭连接。

最好使用其他一些框架机制,可能是ObjectOutputStream / ObjectInputStream。

答案 1 :(得分:0)

String line = null;
while ((line = inFromClientR.readLine()) != null) {
   // do sth
}