@gregoryyoung
First of all I would like to thank you for taking the time to respond to my question. However, whilst that seems to cater for packets not being "fully-read", however, what I need to do is ensure that all data is received, by either slowing down the data transfer or somehow else...
There must be a solution to fully receive all the sent bytes, or implement the data transfer otherwise. Any help that will ensure that data is fully-read, is greatly appreciated!
In the mean time I would still like to thank you for taking the time to reply!
Main Topics
Browse All Topics





by: gregoryyoungPosted on 2009-08-21 at 05:22:05ID: 25151004
if you look at the read method it returns you an integer which represents the number of bytes that were actually read.
sometimes a read will only read a few bytes (depending on the packet structure with your TCP stream).
For this reason most people use a protocol that is either fixed size or includes deliniaters so that they can figure out when they have read a "full packet"
eg:
int bytesread = stream.Read(0, buffer, buffer.Length);
I can be told that only 1 byte was read. If I am reading a 256 byte packet ...
int count = 0;
byte [] buffer = new byte[256];
byte [] packet = new byte[256];
while(true) {
int bytesread= stream.Read(0, buffer, buffer.Length);
if(count + bytesread <=255) {
count += bytesread;
Array.Copy(packet, count, buffer, 0, bytesread);
if(count == 255) {
ProcessMessage(packet);
count = 0;
}
}
if(count + bytesread > 255) {
int touse = 255 - count;
Array.Copy(packet, count, buffer, 0, touse);
ProcessMessage(packet);
count = 0;
Array.Copy(packet, count, buffer, bytesread - touse);
}
}