开发者

java socket writeUTF() and readUTF()

开发者 https://www.devze.com 2023-01-21 13:18 出处:网络
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, the writer/reader stream do

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, the writer/reader stream do the things automatically 开发者_如何学编程for you. Here is an example:

writer.java
writeUTF("Hello");
writeUTF("World");


reader.java
String a=readUTF(); // a=Hello
String a=readUTF(); // b=World

I've tried this code snippet and it works fine. However, I'm wondering whether this kind of coding style is supposed to be working fine. Is there any potential risks of using the socket stream in sequence without explicitly seperating each segment?


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:

  • The maximum length of Strings that can be handled this way is 65535 for pure ASCII, less if you use non-ASCII characters - and you cannot easily predict the limit in that case, other than conservatively assuming 3 bytes per character. So if you're sure you'll never send Strings longer than about 20k, you'll be fine.
  • If the app ever needs to communicate with something else (that's not written in Java), the other side may have a hard time handling the modified UTF-8. For application-internal communication, you don't have to worry though.


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.


java.net.Socket works fine, the stream waits readUTF();

But when using mina's CumulativeProtocolDecoder, it won't, throws java.io.EOFException

0

精彩评论

暂无评论...
验证码 换一张
取 消