正在構建一個tcp偵聽器,該偵聽器將從連接讀取信息並響應一些數據。從TCP流讀取多行
我已經得到了一個單線路輸入工作的程序,但是當我發送多條線路時無法工作。
下面是我用
try {
Socket clientConnection = null;
clientConnection = serverSocket.accept();
clientConnection.setSoTimeout(5 * 1000);
String tmpLine=null;
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
for (tmpLine = inFromClient.readLine(); tmpLine != null; tmpLine = inFromClient.readLine()) {
searchLine += tmpLine;
logMsg = "DBUG : T" + threadName + " readline : " + searchLine;
stubLog.addMessage(logMsg, coreProperties.getLogger(),"DEBUG");
}
} catch (Exception e) {
logMsg = "ERRR : T" + threadName + " : tcpStubWorker: error in getting data : " + e.getMessage();
stubLog.addMessage(logMsg, coreProperties.getLogger(),"ERROR");
e.printStackTrace();
}
的代碼時,我在此消息發送從線束:
String logonMsg = "msg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021\n"
+ "bit 007: 1012165507 \n"
+ "bit 011: 498432 \n"
+ "bit 033: (11)59501100554 \n"
+ "bit 070: 001 \n"
+ "bit 100: (11)59503646554 \n";
我得到這個在日誌中:
DEBUG com.stubby.tcp.tcpStub - 14:33:43:494 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021
DEBUG com.stubby.tcp.tcpStub - 14:33:43:509 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021bit 007: 1012165507
DEBUG com.stubby.tcp.tcpStub - 14:33:43:510 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021bit 007: 1012165507 bit 011: 498432
DEBUG com.stubby.tcp.tcpStub - 14:33:43:534 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021bit 007: 1012165507 bit 011: 498432 bit 033: (11)59501100554
DEBUG com.stubby.tcp.tcpStub - 14:33:43:552 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021bit 007: 1012165507 bit 011: 498432 bit 033: (11)59501100554 bit 070: 001
DEBUG com.stubby.tcp.tcpStub - 14:33:43:603 - DBUG : T00000026 readline : nullmsg: 0800 P3A^SNAT2 >>> S3A^SNAT204 15/10/12 16:55:07.00 rec: 001021bit 007: 1012165507 bit 011: 498432 bit 033: (11)59501100554 bit 070: 001 bit 100: (11)59503646554
ERROR com.stubby.tcp.tcpStub - 14:33:48:627 - ERRR : T00000026 : tcpStubWorker: error in getting data : Read timed out
任何想法爲什麼for循環無法識別tmpLine!= null?
我想我可以減少超時,並使該錯誤警告,但聽起來不太正確!
謝謝。
感謝反饋,同行使用此代碼來發送數據
Socket clientSocket = new Socket("10.52.88.102", 8888);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(logonMsg + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("From server: \"" + modifiedSentence +"\"");
clientSocket.close();
,但仍然得到錯誤:(所以我懷疑我不關閉連接正確??
你不需要初始化你在下一行指定的變量 – EJP