[Java]BufferedReader from socket with \r\n

1.1k views Asked by At

I'm trying to read text from a msn switchboard socket

public String readStream() {
    String temp = null;
    try {
        temp = buffReader.readLine();
        System.out.println("<<< " + temp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return temp;
}
// this method is called
public void print() {
  while (socket.isConnected()) {
      readStream(); 
  }
}

The output should be like this when a message is recieved on msn (according too http://www.hypothetic.org/docs/msn/switchboard/example_session.php)

<<< MSG [email protected] Bob 143\r\n
MIME-Version: 1.0\r\n
Content-Type: text/plain; charset=UTF-8\r\n
X-MMS-IM-Format: FN=Lucida%20Sans%20Unicode; EF=B; CO=ff0000; CS=0; PF=22\r\n
\r\n
Hello.

My output:

<<< MSG [email protected] MYNAME 128
<<< MIME-Version: 1.0
<<< Content-Type: text/plain; charset=UTF-8
<<< X-MMS-IM-Format: FN=Arial; EF=; CO=000000; CS=0; PF=00; RL=0;
<<< 

As you can see I'm not recieving the sended message until the person sends another message.

Output:

<<< MSG [email protected] MYNAME 128
<<< MIME-Version: 1.0
<<< Content-Type: text/plain; charset=UTF-8
<<< X-MMS-IM-Format: FN=Arial; EF=; CO=000000; CS=0; PF=00; RL=0;
<<< 
<<< helloMSG [email protected] MYNAME 127
<<< MIME-Version: 1.0
<<< Content-Type: text/plain; charset=UTF-8
<<< X-MMS-IM-Format: FN=Arial; EF=; CO=000000; CS=0; PF=00; RL=0;
<<< 

So how can this be solved so I can read the whole message?

1

There are 1 answers

2
JTMon On

I think the issue is the fact that the stream does not have a line break after the Hello. Thus readLine is not detecting the end of the line until the the one that comes at the end of the first line of the next message. You need to rely on the length of the message to read a predefined number of bytes in order to get the whole message. Check this site out for some hints: http://www.hypothetic.org/docs/msn/resources/faq.php#howtoparse

EDIT: Logic to be implemented quoted from the above site THE MOST Relevant part is near the end:

Whenever new data from the server arrives over the socket, append it to a string that will be used as a "cache". Each time you receive data, call the cache parser function as well.

The cache parser function will examine the cache variable and look for a newline.

If one is not found, the function will return without doing anything.

If one is found, it will read everything from the beginning of the cache up to the newline and put it into a new variable. It will then remove the command up to (and including) the newline from the beginning of the cache.

The cache parser will look at the first three bytes of the command.

If the command is recognized as a regular command, it will call the handler for that command and loop back to see if there are more commands in the cache.

THIS IS THE MOST RELEVANT PART

If the command is recognized as a payload command, it will look at the specified length of the payload, and compare that to the length of the cache.

If it appears that the entire payload is in the cache, the cache parser will take out the beginning of the cache up to the specified length and store it into a payload variable. It will then call the appropriate handler for the payload function and loop back to see if there are more commands in the cache.

If the length of the cache is smaller than the specified length of the payload, then the cache parser will add the command back into the beginning of the cache and return.