Somewhere around 500 connections when calls (BeginSend calls) are
arround 108,752 and total bytes sent are around 9,496,399 bytes - send
callbacks (EndSend) stop coming in C# asynchronous sockets.
I'm counting how many BeginSend are issued and how many EndSend are
received, and somewhere around 500 connections send callbacks stop
coming even when the count for BeginSend shows that there are pending
sends in the system.
Here is the code:
private void SendData(OStateObject so, string sData)
{
byteData = Encoding.ASCII.GetBytes(sData);
try
{
so.socket.BeginSend(byteData, 0, byteData.Length, 0, new
AsyncCallback(SendCallback), so);
PendingSends = Interlocked.Increment(ref PendingSends);
}
catch(System.ObjectDisposedException)
{
}
catch (SocketException e)
{
DestoryStateObject(so);
}
catch (Exception e)
{
DestoryStateObject(so);
}
}
private void SendCallback(IAsyncResult ar)
{
OStateObject so = (OStateObject)ar.AsyncState;
try
{
MaxSendBytes += so.socket.EndSend(ar);
PendingSends = Interlocked.Decrement(ref PendingSends);
}
catch (SocketException e)
{
PendingSends = Interlocked.Decrement(ref PendingSends);
DestoryStateObject(so);
}
catch (Exception e)
{
PendingSends = Interlocked.Decrement(ref PendingSends);
DestoryStateObject(so);
}
}
Here is code from the timer which executes this code every 2 second.
if (ArrayListConnectedSockets.Count >= 500)
{
//"TBGSends" is a global variable to keep track of maximum number of
sends
string bgMsg = "TestData :ghghe ghghghghgh whrerhehrehreh
hthththththththht eherehherehhee ghghe ghghghghgh whrerhehrehreh
hthththththththht eherehherehhee\n";
OStateObject so;
for (int i=0; i<ArrayListConnectedSockets.Count || TBGSends <= 150000;
i++)
{
so = (OStateObject)ArrayListConnectedSockets[i];
SendData(so, bgMsg);
TBGSends++;
}
if (TBGSends >= 150000) TimerBG.Stop();
}
I'm restricting maximum number of connection to 1000 after reaching
this limit server shuts down every new connection.
Maximum a user can send 5 times within a second. If there are 1024
users connected to each other than the server would make 1047552 send
calls if all users send something to each other with in few seconds. 1
or 2 seconds delay doesn't matter but the server jams during the
communication.
Why the server is jamming and anyway to solve this problem in C#?