Recieving Data (Simple)

Eloff

Freshman
Joined
Mar 20, 2003
Messages
35
Location
Canada
Ok the below code loops through until the if statement evals to false, at which point the data is considered to be all recieved, and is processed. What do I put in this if statement that will evaluate to false when the client has pressed return after typing some text (\r\n?). I want the client to say something, and then hit return at which point I know he has finished what he has to say and can process it. Thanks a lot!

C#:
int BytesRead = networkStream.Read(bytes, 0, (int) bytes.Length);
				
if (check if return was pressed)
{
// There should be more data, so store the data received so far.
sb.Append(Encoding.ASCII.GetString(bytes, 0, BytesRead));
}
else
{ 
// All the data has arrived; put it in response.
ProcessDataReceived() ;
}
 
Last edited by a moderator:
I solved the problem by rewriting the code from the ground up. carriageReturn is a bool value and a private member of the class (this is so its value is maintained after the function returns).

C#:
try
{
int b = -1;
do
{
b = networkStream.ReadByte();

if(b == 13)
{
carriageReturn = true; //this executes only if this char being parsed is a carriageReturn
}
else if(carriageReturn && (char)b == 10)
{
ProcessDataReceived();
break;
}
else 
{
carriageReturn = false; //this char was niether a carriage return, nor a carriage return followed by a new line
}
sb.Append((char)b);
} while (b != -1);
}
 
Last edited by a moderator:
Back
Top