0

I'm writing a protocol. My custom messages consist of header and data part. The header is constant in length and have information about data size. Data can be up to 5 MB, because it can contain data attached files. When my server receives data, the data part of my messages is sometimes corrupt. I check it by saving received bytes in base64 to a text file and compare it to the sent bytes. When the data is corrupt in the base64 text file representation it often have area's where are many 'A' symbols which in utf8 are '�' meaning that something is wrong. Once again I emphasise that it's not always corrupt, it seems random, sometimes it happens more often sometimes less and some portions match with the sent bytes. What can be the reason, am I missing something?

Here is some part of my code(the allBundleData that is the specific data is sometimes 5mln bytes long as I mentioned earlier)

Client side:

TcpClient client = client = new TcpClient(transportAddr, transportServicePort);
NetworkStream stream = client.GetStream();

byte[] headerBundleComplete = new byte[59];
byte[] allBundleData;
...
stream.Write(headerBundleComplete, 0, headerBundleComplete.Length);
stream.Write(allBundleData, 0, allBundleData.Length);

Server side:

TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();

byte[] headerBundle = new byte[59];
stream.Read(headerBundle, 0, 59);
...
byte[] dataBundle = new byte[bundleDataLength];
stream.Read(dataBundle, 0, bundleDataLength);

The received dataBundle is sometimes corrupt.

  • 2
    What did your research show up? This question gets asked on a daily basis, and the cause is always the same: you're ignoring `Read()`'s return value. See also [Why does my client socket not receive what my server socket sends?](http://stackoverflow.com/questions/23713664/why-does-my-client-socket-not-receive-what-my-server-socket-sends). – CodeCaster May 12 '16 at 14:39
  • Yes, it was the problem, thanks. – Russian Gepetto May 12 '16 at 18:09
  • Nice, happy to be of help. – CodeCaster May 12 '16 at 18:09

0 Answers0