

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm sending a string message from my client application to my server application using sockets. I'm using DataOutputStream to send from client and DataInputStream to receive the message in my server. I'm sending one string from the client but I noticed that when it gets to the server, it is sometimes broken into several messages. How do I handle this or what's the best way to handle this?

I can probably read each broken message received and check each character for a delimeter to know that it is the end of one message. But is there a better way to handle this?

share|improve this question
up vote 3 down vote accepted

There is no such thing in TCP as "message". It is a stream-oriented protocol. Of course, at lower levels it is transmitted in separate packets, but you have no way to control it and what you are seeing can be different from those packets. You just read as much as available in the receiving buffer at any particular moment. You may perceive your messages as broken down, but you may as well encounter a situation where several messages arrive as combined into one piece.

So when reading a message you should either use some sort of delimiter to figure out where your message ends, or use a header with message length. If you are sending simple strings, encoding them as UTF-8 and terminating them with null bytes should work fine. For more complicated things you'll need more complicated approach, obviously.

share|improve this answer
This must be the single-most asked and answered question in the networking tag. – caf Jan 4 '11 at 2:36

Though about prefixing your messages with a header? It's very common to send a header with the length and checksum of the transmission. This way you're able to allocate appropriate sized buffers and verify data integrity.

share|improve this answer
Do you really need to verify integrity with TCP? It has checksums of its own at the packet level. – Peter Lawrey Jan 3 '11 at 10:26
@Peter it's quite well known that also TCP checksums can fail over extended transfers on the Internet but it shouldn't affect many applications --… – Steve-o Jan 4 '11 at 5:52
Using custom checksums also help detect Man-In-The-Middle processes altering our application data along the way, without breaking TCP's checksums. – Remy Lebeau Jan 4 '11 at 19:53

A simple example of sending the length first. Note: I check the length when reading as an invalid message length can result in an OutOfMemoryException which can be confusing/alarming.

public static void writeBytes(DataOutput out, byte [] bytes) throws IOException {

public static byte[] readBytes(DataInput in) throws IOException {
    int len = in.readInt();
    if (len < 0 || len > 1 << 24) throw new StreamCorruptedException("Invalid message length "+len);
    byte [] bytes = new byte[len];
    return bytes;
share|improve this answer

Your Answer


