3
public void read(byte[] bytess)
{
        int davar = this.clientSocket.Receive(bytess);
        MemoryStream m = new MemoryStream(bytess);
        BinaryFormatter b = new BinaryFormatter();

        m.Position = 0;
        SPacket information = b.Deserialize(m) as SPacket;

        Image imageScreenShot = information.ScreenShot;

        if (information.Premissionize)
            Premitted = true;

        if (information.Text != "")
        {
            cE.GetMessageFromServer(information.Text);
        }

        if (imageScreenShot == null)
            return;

        Bitmap screenShot = new Bitmap(imageScreenShot);

        cE.UpdatePhoto(screenShot);

        //screenShot.Dispose();
        //Form1.t.Text = forText;
}

I have this read function in the client and when I run it online between 2 lan computers deserialization exception is thrown.

I guess that something delaying all the packet and only part of it has arrived. It said that the binary header is not valid.

How can I make sure in C# that I got the whole packet?

By the way this is TCP

sangram parmar
  • 8,462
  • 2
  • 23
  • 47
Shay Sandler
  • 111
  • 10

1 Answers1

3

The Receive function reads at least one byte and at most as many bytes as have been sent. Right now you assume that a single read will read everything which is not the case.

Deserialize from a new NetworkStream(socket). This allows BinaryFormatter to draw bytes from the socket.

What you wrote there about packets being delayed and such is not accurate. TCP shields you from that.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Theoreticly you are right.but in reality i throws deserialization exception if something intruptingg the sync of the server and the client like a message box. and somewhat it happends when its between 2 computers in the lan after few seconds – Shay Sandler Jun 27 '15 at 10:04
  • so the question is how to prevent it from happening – Shay Sandler Jun 27 '15 at 10:06
  • 1
    Did you read the answer? Receive can return you small amount of data. Then the serializer will complain because it got partial data. This code is incorrect, and it causes the issue. – usr Jun 27 '15 at 10:08
  • i understand the code is incorrect. and i understood your answer. i just want to know how to fix it – Shay Sandler Jun 27 '15 at 10:11
  • I told you how to fix it. Deserialize from a NetworkStream. Why doesn't that work for you? – usr Jun 27 '15 at 10:15
  • ohh okay im soryy ill try – Shay Sandler Jun 27 '15 at 10:16
  • That is the most convenient way to do it. NS is an adapter that makes a socket look like a stream. That's nice. – usr Jun 27 '15 at 10:19
  • ok while i check it in the LAN i would like to mention yeasterday when i cheacked it the problem was not that the recived byte buffer wasnt enougth for all the packet but that 2 packets got into one and that why it did not got deserialize – Shay Sandler Jun 27 '15 at 10:21
  • TCP does not give you access to packets. Receive does not return packets, it returns a boundaryless stream of bytes. – usr Jun 27 '15 at 10:25