views:

250

answers:

1

How do you read an incoming tcp stream until a specific delimiter is found in C#? The only possible solution I have come up with is reading the incoming stream one byte at a time.

+4  A: 

Reading the TCP socket and scanning for a delimiter are two different things.

You can read all available data on a non-blocking socket, into a byte array/string, then scan the byte array for your delimiter. Do whatever else you need to do, including perhaps saving data after the delimiter for the next read attempt.

Its best to use some sort of buffer to add incoming data into so the socket operations don't exactly dictate handling of the data.

mrjoltcola
I'll second that. Reading it it slower won't change what's coming down the pipe to you anyway, it just makes for an inefficient read from the socket/stream.
spender
Yep. Reading from a TCP socket one byte at a time is very inefficient. Read the data from the socket into a byte array and then process the byte array.
Matt Davis