Reputation: 25
what is the best way to read from NetworkStream to some delimiter (for example "\n")
I have following code:
NetworkStream clientStream = tcpClient.GetStream();
var message = new byte[4096];
while (true)
{
int bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
// Exception
}
Response(message);
}
Problem is, that from client sends something like "Some text\n continues on newline" but I would like to answer first on "Some text", then accept next line and send response.
Upvotes: 2
Views: 2881
Reputation: 3811
If you just want to read a line then use StreamReader on your NetworkStream
and call its ReadLine
method:
NetworkStream strm = client.GetStream();
StreamReader reader = new StreamReader(strm);
String line = reader.ReadLine();
Upvotes: 4