Java socket bug on linux (0xFF sent, -3 received)

Posted by Marius on Stack Overflow See other posts from Stack Overflow or by Marius
Published on 2010-05-12T12:48:22Z Indexed on 2010/05/12 12:54 UTC
Read the original article Hit count: 144

Filed under:
|
|

While working on a WebSocket server in Java I came across this strange bug. I've reduced it down to two small java files, one is the server, the other is the client. The client simply sends 0x00, the string Hello and then 0xFF (per the WebSocket specification).

On my windows machine, the server prints the following:

Listening
byte: 0
72 101 108 108 111 recieved: 'Hello'

While on my unix box the same code prints the following:

Listening
byte: 0
72 101 108 108 111 -3

Instead of receiving 0xFF it gets -3, never breaks out of the loop and never prints what it has received.

The important part of the code looks like this:

byte b = (byte)in.read();
System.out.println("byte: "+b);

StringBuilder input = new StringBuilder();
b = (byte)in.read();
while((b & 0xFF) != 0xFF){
 input.append((char)b);
 System.out.print(b+" ");
 b = (byte)in.read();
}
inputLine = input.toString();

System.out.println("recieved: '" + inputLine+"'");
if(inputLine.equals("bye")){
 break;
}

I've also uploaded the two files to my server:

My Windows machine is running windows 7 and my Linux machine is running Debian

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets