简体   繁体   中英

Java TCP socket listener

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 ?

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM