looking to build a tcp listener which will read information from a connection and respond with some data.
I've got the program working on a single line input but when I send multiple lines it doesn't work.
Here is the code I was using
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();
}
When I send in this message from a harness:
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";
I get this in the logs:
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
Any ideas why the for loop is failing to recognise on tmpLine != null?
I suppose I could decrease the timeout and make that error a warning but that doesn't sound quite right!
Thanks.
thanks for feedback, the peer is using this code to send the data
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();
but still getting the error :( so i suspect I'm not closing the connection correctly??
对方没有关闭连接,并且您的读取循环只会在对方断开时停止。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.