2013-04-04 162 views
0

inFromClientR.readLine()永不停息。有任何想法嗎?我忘記了什麼嗎?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'); 
+0

寫完「Transfer completed」消息後,你是否在flush()流? – mthmulders 2013-04-04 09:07:27

+0

是的,我在客戶端做了........ – Nicolaos 2013-04-04 09:09:35

+0

outToServerR.writeBytes(「Transfer completed」+'\ n');永不停止。該行之後的println從不顯示 – Nicolaos 2013-04-04 09:10:53

回答

1

的BufferedReader的readLine#()試圖填補其緩衝區8192個字節,任何換行的regradless找到它同時。由於您打開了連接,接收方將等到1)您發送了8192個字節,或者2)關閉連接。

你最好使用其他的成幀機制,也許是一個ObjectOutputStream/ObjectInputStream。

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