TcpListener 请求泛滥
TcpListener request flood
大家好:我正在尝试确保我的 TcpListener 可以正确处理来自客户端的多个请求。我正在尝试在 for 循环内调用多个 BeginSend 方法,发现 Listener 的输出在我得到的响应数量方面不一致。即,如果我连续调用 BeginSend 20 次,我希望 Listener 给我 20 个响应。有时我收到 2 条,有时 3 条,有时更多,而且看起来,我发送给监听器的消息正在相互附加,而不是被视为单独的实体。
假设我有一个有效的 TcpClient class(见代码),它包装了一个基本的 TcpClient 和一个有效的 TcpListener class(见代码),它通过引发一个响应来自连接的 TcpClient 的消息DataReceived 事件,以接收到的消息作为参数。非常基本的代码
for(int i = 0; i < 10; i++)
{
client.Client.BeginSend(Encoding.ASCII.GetBytes(i.ToString()), 0, i.ToString().Length, SocketFlags.None, null, null);
}
我希望 TcpListener 的输出是
0
1
...
9
相反,我得到的是:
0
123456789
或者,
0
1234
56
789
或者,
....
或 10 条单独消息的一些其他排列
这是监听器的相关代码:
private void ListenForNewClients()
{
Task.Run(async () =>
{
while (true)
{
var tcpClient = await tcpServer.AcceptTcpClientAsync();
clients.Add(tcpClient.Client);
tcpClient.Client.BeginReceive(buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, tcpClient.Client);
}
});
}
和 ReceiveCallback:
private void ReceiveCallback(IAsyncResult ar)
{
var tcpClient = ar.AsyncState as Socket;
try
{
var bytesRead = tcpClient.EndReceive(ar);
if (bytesRead == 0)
{
tcpClient.EndReceive(ar);
}
var data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
DataReceived?.Invoke(this, data);
buffer = new byte[BufferSize];
tcpClient.BeginReceive(buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, tcpClient);
}
catch
{
clients.Remove(tcpClient);
}
}
it appears, that the messages I send to the Listener are being appended to one another, rather than being treated as separate entities.
这就是 TCP/IP 的工作原理。
如果您同时编写客户端和服务器,那么我建议使用 SignalR,它会保留消息边界。
如果您必须使用TCP/IP,那么您将需要某种形式的message framing。
大家好:我正在尝试确保我的 TcpListener 可以正确处理来自客户端的多个请求。我正在尝试在 for 循环内调用多个 BeginSend 方法,发现 Listener 的输出在我得到的响应数量方面不一致。即,如果我连续调用 BeginSend 20 次,我希望 Listener 给我 20 个响应。有时我收到 2 条,有时 3 条,有时更多,而且看起来,我发送给监听器的消息正在相互附加,而不是被视为单独的实体。
假设我有一个有效的 TcpClient class(见代码),它包装了一个基本的 TcpClient 和一个有效的 TcpListener class(见代码),它通过引发一个响应来自连接的 TcpClient 的消息DataReceived 事件,以接收到的消息作为参数。非常基本的代码
for(int i = 0; i < 10; i++)
{
client.Client.BeginSend(Encoding.ASCII.GetBytes(i.ToString()), 0, i.ToString().Length, SocketFlags.None, null, null);
}
我希望 TcpListener 的输出是
0
1
...
9
相反,我得到的是:
0
123456789
或者,
0
1234
56
789
或者,
....
或 10 条单独消息的一些其他排列
这是监听器的相关代码:
private void ListenForNewClients()
{
Task.Run(async () =>
{
while (true)
{
var tcpClient = await tcpServer.AcceptTcpClientAsync();
clients.Add(tcpClient.Client);
tcpClient.Client.BeginReceive(buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, tcpClient.Client);
}
});
}
和 ReceiveCallback:
private void ReceiveCallback(IAsyncResult ar)
{
var tcpClient = ar.AsyncState as Socket;
try
{
var bytesRead = tcpClient.EndReceive(ar);
if (bytesRead == 0)
{
tcpClient.EndReceive(ar);
}
var data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
DataReceived?.Invoke(this, data);
buffer = new byte[BufferSize];
tcpClient.BeginReceive(buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, tcpClient);
}
catch
{
clients.Remove(tcpClient);
}
}
it appears, that the messages I send to the Listener are being appended to one another, rather than being treated as separate entities.
这就是 TCP/IP 的工作原理。
如果您同时编写客户端和服务器,那么我建议使用 SignalR,它会保留消息边界。
如果您必须使用TCP/IP,那么您将需要某种形式的message framing。