您将在下面找到我的代码。问题是,这应该是一个“客户端聊天”程序,因此您可以不断发送消息。但是,不知何故,该应用程序被 console.readline() 阻止,因此提示用户只输入一次消息。有什么解决方法吗?可能正在使用预定义的读取缓冲区?
static void Main(string[] args)
{
TcpClient client = new TcpClient("127.0.0.1", 7777);
try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
//Console.WriteLine(sr.ReadLine());
while (true)
{
Console.Write("Message: ");
string msg = Console.ReadLine();
sw.WriteLine(msg);
if (msg == "chao") break;
Console.WriteLine(sr.ReadLine());
}
s.Close();
}
finally
{
client.Close();
}
}