当我很快地从客户端多次发送数据时,我不会在服务器中单独接收这些数据。我收到它们作为一个大数据突发。我需要为每个收到的数据发送回复。但我不能这样做,因为这些数据加入了一个大爆发。如何为每个收到的数据发送回复? 这里是服务器中回调方法的代码:
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());
}
}
答案 0 :(得分:3)
如果您的套接字是TCP(我无法从代码中得知),这是预期的行为,因为TCP不像UDP那样被构建。您需要自己划分数据。