TCP Connection Failures (WSAEHOSTUNREACH)

On a local gigabit network, I have an application that uses one TCP server and many clients. Each client connects the server every 30 seconds, opening a TCP connection, sends it a status message and closes.

The server is configured using SocketAsyncEventArgsvery similar to the example shown HERE (omitted for brevity)

The client initiates the connection using TcpClient.

Corresponding client code section:

using (TcpClient client = new TcpClient())
{
     IAsyncResult ar = client.BeginConnect(address, port, null, null);
     if (!ar.AsyncWaitHandle.WaitOne(timeout))
     {
         throw new ApplicationException("Timed out waiting for connection to " + address);
     }
     client.EndConnect(ar); //exception thrown 5%-10% of the time

     //...send message and receive response...
 }

Everything works fine, except that on some machines exception selected only 5% -10% of the time on EndConnect.

The exception is WSAEHOSTUNREACH(10065):

System.Net.Sockets.SocketException (0x80004005): A socket operation was attempted to an unreachable host 192.168.XXX.XXX:XXXX
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.TcpClient.EndConnect(IAsyncResult asyncResult)
  • , , , , .
  • , EndConnect BeginConnect, ar.AsyncWaitHandle.WaitOne .

, ? .

+3
1

, , . , .

SetThreadExecutionState, , , .

, , SocketExceptions . , , , .

0

All Articles