我正在Windows 10上运行NetCore,我有两个程序——服务器和客户端,我都在本地运行。
执行顺序如下:
- 运行服务器 - 有一个循环处理客户端 - 运行客户端 - 客户端完成工作并终止进程 - 第二次运行客户端
这个顺序使用下面的代码会产生以下来自服务器的输出:
换句话说,第二次尝试运行客户端时会出现异常。这就是奇怪的地方。
以下是我用于重现此问题的最小代码示例。
服务器代码:
执行顺序如下:
- 运行服务器 - 有一个循环处理客户端 - 运行客户端 - 客户端完成工作并终止进程 - 第二次运行客户端
这个顺序使用下面的代码会产生以下来自服务器的输出:
Waiting for client.
Reading message.
Incoming message: This message is longer than what
Sending message.
Closing connection.
Waiting for client.
Reading message.
Incoming message: This message is longer than what
Sending message.
Closing connection.
Waiting for client.
并且客户端输出如下:
Connecting to server.
Sending message.
Reading message.
Incoming message: Thank you!
Connecting to server.
Sending message.
Reading message.
Unhandled Exception: System.AggregateException: One or more errors occurred. (Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.) ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
换句话说,第二次尝试运行客户端时会出现异常。这就是奇怪的地方。
以下是我用于重现此问题的最小代码示例。
服务器代码:
public static async Task StartServerAsync()
{
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
listener.Server.NoDelay = true;
listener.Server.LingerState = new LingerOption(true, 0);
listener.Start();
while (true)
{
Console.WriteLine("Waiting for client.");
using (TcpClient client = await listener.AcceptTcpClientAsync())
{
client.NoDelay = true;
client.LingerState = new LingerOption(true, 0);
Console.WriteLine("Reading message.");
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = new byte[32];
int len = await stream.ReadAsync(buffer, 0, buffer.Length);
string incomingMessage = Encoding.UTF8.GetString(buffer, 0, len);
Console.WriteLine("Incoming message: {0}", incomingMessage);
Console.WriteLine("Sending message.");
byte[] message = Encoding.UTF8.GetBytes("Thank you!");
await stream.WriteAsync(message, 0, message.Length);
Console.WriteLine("Closing connection.");
}
}
}
}
客户端代码:
public static async Task StartClientAsync()
{
using (TcpClient client = new TcpClient())
{
client.NoDelay = true;
client.LingerState = new LingerOption(true, 0);
Console.WriteLine("Connecting to server.");
await client.ConnectAsync("127.0.0.1", 1234);
Console.WriteLine("Sending message.");
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = Encoding.UTF8.GetBytes("This message is longer than what the server is willing to read.");
await stream.WriteAsync(buffer, 0, buffer.Length);
Console.WriteLine("Reading message.");
int len = await stream.ReadAsync(buffer, 0, buffer.Length);
string message = Encoding.UTF8.GetString(buffer, 0, len);
Console.WriteLine("Incoming message: {0}", message);
}
}
}
有趣的是,如果我们将客户端消息从
改为标签后,服务器不会接收到消息。这是因为HTTP协议要求请求消息必须以两个连续的新行作为结束符,而更改了消息内容后未正确结束消息。"This message is longer than what the server is willing to read."
to
"This message is short."
两个客户端实例都不会崩溃并且产生相同的输出。
请注意,如果我省略了设置LingerState的三行代码,则没有任何区别。如果我将LingerState设置为new LingerState(true, 5)
,则行为没有任何区别。
同样,如果我将NoDelay设置为false也不会有任何区别。换句话说,在任一侧设置任何值的LingerState和NoDelay似乎对崩溃没有任何影响。唯一防止崩溃的方法是在服务器端从客户端读取所有输入。
这很奇怪,我想知道是否有人能够解释一下。我不确定它是否也适用于.NET Framework,只测试过NetCore 1.0.0和Windows 10。