I have simple TcpListener which accepts a pending connection and I read data from NetworkStream with using a StreamReader.ReadLineAsync.
TcpClient writes data to the stream.
According to documentation
A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n")
The problem is that StreamReader.ReadLineAsync ignores the carriage return and never reads the line.
Server:
static async Task Main(string[] args)
{
var listener = TcpListener.Create(22);
listener.Start();
var client = await listener.AcceptTcpClientAsync();
using (var networkStream = client.GetStream())
{
StreamReader sr = null;
StreamWriter sw = null;
try
{
sr = new StreamReader(networkStream, leaveOpen: true);
sw = new StreamWriter(networkStream, leaveOpen: true);
while (!sr.EndOfStream)
{
var clientData = await sr.ReadLineAsync();
...
}
}
catch (Exception ex)
{
...
}
finally
{
sr?.Close();
sw?.Close();
}
}
}
Client:
static async Task Main(string[] args)
{
// Server ReadLineAsync hangs for CR
var data = new byte[] { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 13 };
// Server ReadLineAsync works for LF
// var data = new byte[] { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 10 };
var client = new TcpClient("localhost", 22);
using (var stream = client.GetStream())
{
await stream.WriteAsync(data, 0, data.Length);
Thread.Sleep(5000);
}
}
This client is only for testing purposes. The real client will be an external application which will send data ended by CR.