0

My Context

I have a TCP networking program that sends large objects that have been serialized and encoded into base64 over a connection. I wrote a client library and a server library, and they both use NetworkStream's Begin/EndReadandBegin/EndWrite. Here's the (very much simplified version of the) code I'm using:

For the server:

var Server = new TcpServer(/* network stuffs */);
Server.Connect();
Server.OnClientConnect += new ClientConnectEventHandler(Server_OnClientConnect);

void Server_OnClientConnect()
{
    LargeObject obj = CalculateLotsOfBoringStuff();
    Server.Write(obj.SerializeAndEncodeBase64());
}

Then the client:

var Client = new TcpClient(/* more network stuffs */);
Client.Connect();
Client.OnMessageFromServer += new MessageEventHandler(Client_OnMessageFromServer);

void Client_OnMessageFromServer(MessageEventArgs mea)
{
    DoSomethingWithLargeObject(mea.Data.DecodeBase64AndDeserialize());
}

The client library has a callback method for NetworkStream.BeginRead which triggers the event OnMessageFromServer that passes the data as a string through MessageEventArgs.

My Problem

When receiving large amounts of data through BeginRead/EndRead, however, it appears to be fragmented over multiple messages. E.G. pretend this is a long message:

"This is a really long message except not because it's for explanatory purposes."

If that really were a long message, Client_OnMessageFromServer might be called... say three times with fragmented parts of the "long message":

"This is a really long messa"

"ge except not because it's for explanatory purpos"

"es."

Soooooooo.... takes deep breath

What would be the best way to have everything sent through one Begin/EndWrite to be received in one call to Client_OnMessageFromServer?

3
  • What would be the best way to have everything sent through one Begin/EndWrite received through one Begin/EndRead? No way.
    – L.B
    Aug 12, 2012 at 23:04
  • 1
    Is this not the entire question around the internet? Just one big burst to send everything?! You will never be able to guarantee that everything is sent in one payload.. in the end, it's not up to your code.. Aug 12, 2012 at 23:07
  • @JoachimPileborg, I think you should post that as an answer. This question comes up once a week.
    – usr
    Aug 12, 2012 at 23:13

2 Answers 2

6

You can't. On TCP, how things arrive is not necessarily the same as how they were sent. It the job of your code to know what constitutes a complete message, and if necessary to buffer incoming data until you have a complete message (taking care not to discard the start of the next message I the process).

In text protocols, this usually means "spot the newline / nul-char". For binary, it usually means "read the length-header in the preamble the the message".

4

TCP is a stream protocol, and has no fixed message boundaries. This means you can receive part of a message or the end of one and the beginning of another.

There are two ways to solve this:

  1. Alter your protocol to add end-of-message markers. This way you continuously receive until you find the special marker. This can however lead that you have a buffer containing the end of one message and the beginning of another which is why I recommend the next way.
  2. Alter protocol to first send the length of the message. Then you will know exactly how long the message is, and can count down while receiving so you won't read the beginning of the next message.
3
  • For option 2, I would assume you would send the data length as the first byte... what should I do if the data is longer than 255 characters? (in my case, it inevitably will be)
    – Entity
    Aug 12, 2012 at 23:27
  • If you follow @ColeJohnson suggestion make sure you can handle the edge case of your stream being split in the middle of your length header. Aug 13, 2012 at 0:28
  • 1
    Thats why I send a header of a header. That contains the length of the header. Redundant, but its only 8 bytes (I use long, not int)
    – Cole Tobin
    Aug 13, 2012 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.