I\'ve been reading some Java socket code snippet and fonund out a fact that in socket communication, to send messages in sequence, you don\'t have to seperate them by hand,
java.net.Socket
works fine, the stream waits readUTF();
But when using mina's CumulativeProtocolDecoder
, it won't, throws java.io.EOFException
According to the documentation the readUTF
and writeUTF
methods work with a modified version of UTF8 that also adds the length of the character to be read in the beginnig.
This should mean that the read operation will wait until enough characters had been fetched before returning the string.. this means they are actually segmented also if you don't see it since you merely decorate the streams of the socket with the DataInputStream
and DataOutputStream
.
In conclusion, yes, it should be quite safe, since the API itself will take care of separating the single messages.
The writeUTF()
and readUTF()
write the length of the String (in bytes, when encoded as UTF-8) followed by the data, and use a modified UTF-8 encoding. So there are some potential problems: