简体   繁体   English

Java TCP流终止

[英]Java TCP stream termination

I am trying to read from a tcp stream a message sent from client. 我正在尝试从tcp流中读取客户端发送的消息。 Thing is after reading the last line, my readline function is not returning null and i am not able to debug why, as control point is lost. 事情是在读取最后一行之后,我的readline函数没有返回null,而且由于失去控制点,我无法调试原因。 In short, after last line read, readLine function should return null, but i am not getting any thing as such. 简而言之,在最后一行读取之后,readLine函数应该返回null,但是我没有得到任何类似的东西。

this is what my code looks like 这就是我的代码的样子

        StringBuffer sipBuffer = null;
        String lineRead = null;
        readIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        try {
                sipBuffer = new StringBuffer();
                while ((lineRead = readIn.readLine()) != null) {
                    sipBuffer.append(lineRead);

                    sipBuffer.append("\n");
                }
            } catch (Exception ex) {
                sipConsole.addText("Error in message: \n");
                sipConsole.addText(ex.getMessage());
                return;
            }

format of message is 邮件格式为

    String inviteReq = "INVITE sip:" + destIP + "@sip.umsy.edu SIP/2.0" + "\n"
            + "From: \"" + myName + "\" <sip:" + myIP + "@sip.umsy.edu>" + "\n"
            + "To: <sip:" + destIP + "@sip.umsy.edu>" + "\n"
            + "Allow: INVITE, ACK, BYE" + "\r\n";

readLine() will return null when the stream is closed. 流关闭时,readLine()将返回null。 Are you really sure the stream gets closed ? 您确定要关闭流吗?

It seems you're reading a SIP message in which case the other end probably won't close the connection until the call is complete- or atleast until it's either answered or terminated before it gets answered. 似乎您正在阅读SIP消息,在这种情况下,另一端可能直到呼叫完成才关闭连接,或者至少直到它被应答或在被应答之前终止为止。 You'll have to follow the SIP protocol, and handle messages on the stream according to the SIP spec. 您必须遵循SIP协议,并根据SIP规范处理流中的消息。

Has the client closed the stream it is writing to? 客户端是否关闭了正在写入的流? If it hasn't, your readLine() is blocking for more data from the client. 如果还没有,则您的readLine()会阻止从客户端获取更多数据。 Client-server protocols typically establish a mechanism (other than stream close) to indicate size. 客户端-服务器协议通常建立一种机制(而不是流关闭)来指示大小。 Whether it is a content-length or through a special token. 无论是内容长度还是通过特殊令牌。

请检查您是否在客户端刷新并关闭OutputStream,以免InputStreamReader阻塞并在服务器端永远等待?

Be sure to close the connection on the client side once the message is sent. 发送消息后,请确保在客户端上关闭连接。 Otherwise, the server side will block and wait for more data. 否则,服务器端将阻塞并等待更多数据。

I suspect that the client did not close the socket after sending the message, so the server is blocked waiting for new lines. 我怀疑客户端在发送消息后没有关闭套接字,因此服务器被阻止等待新行。 Is this the case? 是这样的吗?

Remember that readline() returns null only if the end of stream is reached. 请记住,只有到达流的末尾时,readline()才返回null。 If your protocol requires a constantly open connection between the client and the server, you will have to detect the end of message in a different way. 如果您的协议要求客户端和服务器之间的开放连接不断,则您将必须以其他方式检测消息的结尾。

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

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