views:

914

answers:

5

I am writing a small Java server, and a matching client in C++, which implement a simple IM service over the STOMP protocol.

The protocol specifies that every frame (message that passes between server and client, if you will) must end with a null character, which in code I refer to as '\0', both in Java and in C++.

However, when I transmit a frame over TCP via sockets, the null character simply does not show up, on either side. I am working with UTF-8 encoding, and tried switching to ASCII, didn't help.

What am I doing wrong?

+1  A: 

I'd recommend downloading Wireshark and monitoring the transmission to see if the problem is on the sending or the receiving end.

Spencer Ruport
A: 

Are you transmitting a buffer or a string? If you transmit a string, the null character will be terminating the string and won't be transmitted. Using a buffer, you can specify how many bytes you want to transmit and include the null character.

Of course, the problem can be both on the transmission and the receiving side.

schnaader
+2  A: 

Wether you are encoding text in ASCII or UTF-8, you convert your "letters" to a stream of bytes (byte encodings). You need to add a ZERO byte to the end of the message strings.

[Guessing] You may be using a high-level library with a method like "WriteLine(String line)" to send the data over the network. The documentation for that method with describe what bytes are actually sent, which typically includes the message text encoding in the current encoding (ASCII, UTF-8, etc) followed by a line termination sequence, which is typically either the byte 13, 10m or a combination of them ('\n', '\r\n').

Use the low-level Write() method or WriteBytes() method (depending on your libraries). Convert the text to ASCII or UTF-8, add the zero byte to the end, and send exactly what you want to send.

Bill
A: 

The first thing you need to do is use Wireshark (or something similar) as suggested by Spencer.

If it's a transmit-side issue, double check that you are encoding the message properly by adding appropriate diagnostic traces to your code.

If it's a receive-side issue, is there a way to set up the delimiting character on the receive socket? There might be a socket option that says whether to include or exclude the delimiting character. Maybe it's being transmitted properly, but the receive socket is stripping it off.

LeopardSkinPillBoxHat
A: 

You are in C++?

A newbie mistake is putting the NUL at the end of the string: "Foo!\x00".

Of course, the ting that writes the string treats that first NUL as the end of the string, and dos not transmit it. You need to white the nul character '\x00' explicitly as a character (with putChar or however c++ does it), not as part of a string.

paulmurray