NetworkStream DataAvailable, ReadByte() is slow

Joined
Jul 4, 2005
Messages
19
Reaction score
0
Good Day,
I had problems with speed of my data receiving function. My function is accepting data from Network Stream.

In simplified version it looked:

//Receive Data from NetworkStream until char 13 arrive
private string ReceiveData(NetworkStream NS)
{
StringBuilder SB = new StringBuilder(512);
while(true)
{
if(NS.DataAvailable)
{
byte OneByte = NS.ReadByte();
if(OneByte == 13)
break;
SB.Append((char)OneByte);
}
else
Thread.Sleep(10);
}
return SB.ToString();
}

I had big problems with speed of this function. I could accept only about 1000B/s on my Pocket PC. Problems are in "NS.DataAvailable" and "OneByte = NS.ReadByte()".

Now I solved this problems by using Array of incoming bytes, but I would like to ask You why is reading Byte by Byte so slow (I thought that it is only iteration of position in incoming data stream) . And Why is testing "DataAvailable" so slow?

Thank You
 
Back
Top