How to ensure that the entire package is received
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 on a network between two lan computers, a deserialization exception is thrown.
My guess is that something is delaying the entire package, and only a fraction of its profits. He said the binary header is not valid.
How can I make sure that in C # I got the whole package?
By the way, this is TCP
source to share
The Receive function reads at least one byte and no more than as many bytes as were sent. Right now, you are assuming that one reading will read everything that is wrong.
Disconnect from a new NetworkStream(socket)
. This allows you BinaryFormatter
to draw bytes from the socket.
What you wrote there about packet delays is inaccurate. TCP protects you from this.
source to share