StreamReader.ReadLine no funciona a través de TCP cuando se usa \ r como terminador de línea
Cuando uso solo\r
como terminador de línea, elStreamReader.ReadLine()
l método @ no funciona. Funciona si usoEnvironment.NewLine
, \r\n
o\ra
("a" es cualquier personaje). ¿Es esto un error? El mismo problema no ocurre cuando se usa MemoryStream en lugar de NetworkStream. ¿Qué soluciones alternativas puedo usar si no puedo cambiar el protocolo?
Mi servicio
Thread service = new Thread((ThreadStart)delegate
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 2051);
listener.Start();
using (TcpClient client = listener.AcceptTcpClient())
using (NetworkStream stream = client.GetStream())
using (StreamReader reader = new StreamReader(stream))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.AutoFlush = true;
string inputLine = reader.ReadLine(); // doesn't work - freezes here
writer.Write("something\r");
}
});
service.Start();
Mi client
using (TcpClient client = new TcpClient("localhost", 2051))
using (NetworkStream stream = client.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader = new StreamReader(stream))
{
writer.AutoFlush = true;
writer.Write("something else\r");
var message = reader.ReadLine(); // doesn't work even
// if I remove the freezing point above
}