gpt4 book ai didi

C# 接收数据的问题

转载 作者:行者123 更新时间:2023-11-30 22:39:01 25 4
gpt4 key购买 nike

当我非常快速地从客户端多次发送数据时,我不会在服务器中单独接收这些数据。我将它们作为一个大数据突发接收。我需要为每个收到的数据发送回复。但我做不到,因为这些数据会形成一个大爆发。如何为每个收到的数据发送回复? 服务端回调方法代码如下:

    private void RecieveCallback(IAsyncResult asyncResult)
{
ConnectionInfo connection = (ConnectionInfo)asyncResult.AsyncState;
try
{
int bytesRead = connection.Socket.EndReceive(asyncResult);

if (bytesRead > 0)
{
for (int i = 0; i < bytesRead; i++)
connection.FullBufferReceive.Add(connection.BufferReceive[i]);
if (bytesRead == connection.BufferReceive.Length)
{
connection.Socket.BeginReceive(connection.BufferReceive, 0, connection.BufferReceive.Length, 0,
new AsyncCallback(RecieveCallback), connection);
Console.WriteLine("Bytes recieved -- " + bytesRead + " by " + connection.Id);
}
else
{
Console.WriteLine("Bytes recieved " + bytesRead + " by " + connection.Id);

_serverController.StartProcess(connection);
}
}
else
CloseConnection(connection);
}
catch (Exception e)
{
CloseConnection(connection);
Console.WriteLine(e.ToString());
}
}

最佳答案

如果您的套接字是 TCP(我无法从代码中看出),这是预期的行为,因为 TCP 不像 UDP 那样被构造。您需要自己对数据进行定界。

关于C# 接收数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5902217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com