3

I'm trying to write a WebSocket Server in both, java and C++ but I'm stuck right now. Using java and java.net.ServerSocket/java.net.Socket I managed to get a connection and succesfully do the handshake but the data sent by the WebSocket to the Java Server is not quite what I expected.

When sending messages from javascript like this:

var count = 0;
function loop(){
    websocket.send("loop: " + count + "\n");
    count++;

    setTimeout(loop, 100);
}

The Java server receives this, with line feeds every now and then but not for every websocket.send() that has been invoked.

?‡½÷"˜Ñ˜Mè‡×?‡AÎ3-¡C{îN?‡ŒÍ[Uà¢4%¶íi?‡$ÍåøH¢ŠˆíÖ?‡·†ÞžÛé±î?¦ê?‡'½Ø…KÒ·õ?í?‡dÒÛ‘
½´á^òí?‡+ù?YG–â)Ùº?‡›?
Ë÷àb»¡¯5?‡mÉŒQ¦ã!Wéµ?ˆ:J FV%f6

The Java server retrieves values from the socket using BufferedReader.readLine()

BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line =socketReader.readLine();

This works fine for the handshake and all handshake data is readable but it does not work after the handshake is done.

Is the data after the handshake somehow encrypted? How can I read it?

EDIT :

The program files:

SocketConnectTest.html
ServerTest.java
ClientSessionTest.java
ResponseGenerator.java
output

Just run ServerTest.java and then open SocketConnectTest.html.
ClientSessionTest.initClientListener() handles the messages from the client.

SOLUTION : For the solution see pimvdbs post below and his answer at How to (de)construct data frames in WebSockets hybi 08+?

Community
  • 1
  • 1
Markus
  • 2,174
  • 2
  • 22
  • 37
  • Can you try using telnet to your server? This will allow you to type plain text and see exactly what is being sent and the respone (You can even do this with HTTP servers ;) – Peter Lawrey Oct 26 '11 at 09:01
  • It could be compressed; does your `accept-encoding` header include `gzip`? If so, remove it and try again. – trojanfoe Oct 26 '11 at 09:03
  • @trojanfoe I tried to remove gzip and also replace it with identity. Neither helped. – Markus Oct 26 '11 at 10:12

1 Answers1

4

The data coming across web sockets is raw, not string encoded data.

I'd suggest not wrapping a BufferedReader around the incoming data as packets are framed with 0x00 bytes. The crazy characters you are seeing are a result of Java not understanding the encoding that the data is in.

You will need to be responsible for splitting up the data into character and control parts. Once you've split the data up into the appropriate areas, then you can decode the data as a string.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • I tried to read it with socket.getInputStream().read(buff); now. Everytime read() retrieves a byte array, the first 2 bytes are -127 and -123. The following bytes, however, appear to be random even if I always send the same message. – Markus Oct 26 '11 at 10:15
  • We'd probably need to see how you are sending and the message to go into that one in more detail. For example, when you send are you wrapping the data correctly? – Jeff Foster Oct 26 '11 at 10:41
  • I've added the sources to the initial posting. Are messages supposed to be wrapped somehow? Tutorials I've seen always send raw strings with send() so I thought I'd start with this too and make my own application protocol once sending works. – Markus Oct 26 '11 at 10:56
  • 3
    Data is encoded and the keys are random, and so is the encoded data (as a result). The first two bytes contain some header information and the length of the data, which you don't necessarily need. You need to decode the data bytes using the key (mask) bytes, using the algorithm defined in the specification (I posted some pseudocode previously at http://stackoverflow.com/questions/7040078/not-sure-how-to-frame-data-in-websockets-draft-08/7045885#7045885). – pimvdb Oct 26 '11 at 11:24
  • @pimvdb I wish I could upvote your answer multiple times. Works like a charm, thank you! And I've also learned that java 7 has binary literals as in 0b1000_000. Great start in the day. – Markus Oct 29 '11 at 08:33