hi, i want to read stream from networkstream as below...
in the below code myReadBuffer.length is is 1024.i want to read stream of 1024 bytes length into
myReadBuffer....but i am getting different bytes length into myReadBuffer some times 50 bytes,some times 360 bytes....if i put breakpoint and Thread.sleep(),it is working some times good(giving some more extra bytes into myReadBuffer).but if i run without breakpoint and without thread.sleep(2000) it is not giving output....any one can explain why it is so.....
if(myNetworkStream.CanRead){
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do{
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while(myNetworkStream.DataAvailable);
// Print out the received message to the console.
Console.WriteLine("You received the following message : " +
myCompleteMessage);
}
else{
Console.WriteLine("Sorry. You cannot read from this NetworkStream.");
}