I have a server that sometimes does not accept new connections. The issue occurs when I open multiple TCP connections but not always.
The server is deployed in multiple remote places (cloud) to test under different RTT. When I open single TCP connection, everything works as expected, but when I try 2 or more TCP connections from the same client to the server (on different ports), the connection fails.
In particular, I have tested with up to 5 TCP connections, locally and by deploying the server on the cloud (with RTT < 5ms) and works, but when the server is located on a server with RTT > 75ms, the connection on the second port most of the time (~90%) is not accepted.
I believe that it has to do with the high RTT, given that this is the only difference between the two locations. Once I set the blocking mode to true, then it works, but I can see that this significantly degrades the performance of the app (due to the blocking).
This the code:
List<Socket> _socket = new List<Socket>();
List<int> _Ports = new List<int> (3) { 48005, 48006, 48007 };
void FunServer ()
{
for (int ii = 0; ii < _Ports.Count; ii++)
{
_socket .Add(new Socket(SocketType.Stream, ProtocolType.Tcp));
_socket [ii].Blocking = false;
IPEndPoint _endPoint = new IPEndPoint(IPAddress.Any, _Ports [ii]);
_socket[ii].Listen(10);
_socket [ii].Bind(_endPoint);
}
Thread _listeningThread = new Thread(this.ListeningFun);
_listeningThread.IsBackground = true;
_listeningThread.Start();
}
void ListeningFun()
{
while (true)
{
for (int ii = 0; ii < _Ports.Count; ii++)
{
Socket _newClient = null;
try
{
_newClient = oServerSocket[ii].Accept();
}
catch (SocketException e)
{
if (e.SocketErrorCode != SocketError.WouldBlock)
{
\\it never enters here
}
else
{
break; // when fails this the error received is: A non-blocking socket operation could not be completed immediately
}
}
// some other code here ...
}
Thread.Sleep(10);
}
}
If I set the blocking mode to true, it will work, but the performance is lower for the app.
My question is, since I check every 10ms, why can't read the second time (or third etc.)?
I have Wireshark running on both machines, and I can see that the packets arrive on both ports unharmed.
How should I deal with this one? Should I use Async operations?