Reputation: 43
When I send data from the client side many times one after another very quickly, i don't receive these data separately in the server. I receive them as one large data burst. I need send the reply for every received data . But i can't do it, because these data join in one large burst. How can I send reply for every received data? Here the code of callback method in the server:
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());
}
}
Upvotes: 2
Views: 266
Reputation: 1651
If your sockets are TCP (I can't tell from the code), this is expected behavior as TCP isn't framed like UDP is. You need to delimit the data yourself.
Upvotes: 3