tags:

views:

89

answers:

5

hi

I am trying to read from a tcp stream a message sent from client. 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. In short, after last line read, readLine function should return null, but i am not getting any thing as such.

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";
A: 

Please check if you are flushing and closing the OutputStream on the client side to not have your InputStreamReader blocked and waiting forever on the server side?

Promotos
+1  A: 

readLine() will return null when the stream is closed. 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. You'll have to follow the SIP protocol, and handle messages on the stream according to the SIP spec.

nos
+1  A: 

Has the client closed the stream it is writing to? If it hasn't, your readLine() is blocking for more data from the client. 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.

Dilum Ranatunga
A: 

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.

Marcus Adams
A: 

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. 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.

Eyal Schneider