We need to implement a Socket client which should connect to a server which accepts TCP connections. If i communicate via netcap with the server i get immediate responses from it (via command line).
The workflow is:
nc 99.0.99.84 20000
then i send a connection request to the server
*99*0##
I get the ACK response back
*#*1##
I send my request
*#18*802*86##
I get the response back
*#18*802*86*222241400##*#*1##
everything is very quick via command line.
So i was trying to do this with a Socket client in this way
try {
Socket socket = new Socket("99.0.99.84", 20000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Start");
Thread.sleep(1000);
String connectionRequest ="*99*0##";
System.out.println("Sending connection request " + connectionRequest);
out.println(connectionRequest);
String connResponse = in.readLine();
System.out.println("Response to connection is " + connResponse);
Thread.sleep(500);
String payload ="*#18*802*86##";
System.out.println("Sending " + payload);
out.println(payload);
String response = in.readLine();
System.out.println("Response is " + response);
out.close();
in.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
When using it, the client takes the connection response in a lot of time then quits with response = null
Sending connection request*99*0##
Response to connection is *#*1##*#*1##
Sending *#18*802*86##
Response is null
Is there something wrong?
\r,\nor\r\n?readLinewill read until that point, and wait until it gets it.