1

I have very simple TCP socket listener. I want to stop receiving data when the character is null or #.

while ((line = in.readLine()) != null && !line.equals("#")){
    tcpData = tcpData + line;
    }
server.close();

is not closing the socket as code flows. The socket closed only when I am closing from Hyperterminal. What I missed ?

4
  • 1. Is the "#" sent by server? 2. Does the server end that line? 3. Does the server close the connection? Commented Nov 24, 2015 at 8:35
  • 2
    Don't use readLine unless you want to read a line. Commented Nov 24, 2015 at 9:25
  • What can I use instead readline ? Commented Nov 24, 2015 at 14:09
  • Have you considered consulting the Javadoc? There are three other read methods. Commented Nov 24, 2015 at 17:33

1 Answer 1

1

Because readLine() only returns null at end of stream, which only occurs in TCP sockets when the peer has closed the connection.

... and clearly you are never sending a line consisting of # plus a line terminator.

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

2 Comments

Remote terminal sending sensor data . For example string as $353323058181636,EV,D,T,567888.9,+12C,FFFFE000# which $ is start character and # is end of data character. Each packet coming every minute. In my case when data ends socket need close the socket.
So you're reading lines but it isn't sending lines. So don't use readLine().

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.