0

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?

3
  • Does the response end with either \r, \n or \r\n? readLine will read until that point, and wait until it gets it. Commented Oct 19, 2017 at 11:13
  • no... there is no end of line element... Commented Oct 19, 2017 at 11:15
  • thanks to your comment i solved using a read() by reading char per char Commented Oct 19, 2017 at 11:55

1 Answer 1

1

As you can see here : https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

readLine() will return null if the stream was terminated before it reaches an End Of Line feed like '\n' or '\r'.

Just like in your case, you dont send an EOL and then you close the stream thus returning the null.

Try adding '\n' at the end of your msg.

hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the problem is i cannot change how the server sends its messages..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.