0

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');
4

2 回答 2

1

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

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

于 2013-04-04T09:30:08.993 回答
0
String line = null;
while ((line = inFromClientR.readLine()) != null) {
   // do sth
}
于 2013-04-04T12:02:44.693 回答