简体   繁体   English

Java TCP套接字侦听器

[英]Java TCP socket listener

I have very simple TCP socket listener. 我有一个非常简单的TCP套接字侦听器。 I want to stop receiving data when the character is null or # . 我想在字符为null#时停止接收数据。

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. 因为readLine()仅在流的末尾返回null,这仅在对等方关闭连接时在TCP套接字中发生。

... and clearly you are never sending a line consisting of # plus a line terminator. ……显然,您永远不会发送由#加行终止符组成的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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