Socket.Connect gernerates Execption A socket operation was attempted to an unreachable network.

Nathan Lyon 1 Reputation point

I've seen quite a few other posts on this exception, but none of them helped me to resolve my issue. I can make the connection just fine using telnet, but when my application attempts to connect to the same address and port, I get the A socket operation was attempted to an unreachable network, exception. The environment the application is running in does not allow ICMP traffic and that seems to be the only thing different. The route exists from the server to the default gateway, and I've been assured that the route is known by the gateway. So why am I unable to make this connection?

You can see the Telnet traffic captured by wireshark here:

But the application gets an exception:
and I see nothing in the wireshark capture.

There's nothing interesting about my code, I've stripped it out of the larger application just to test the TcpClient.Connect() (I've also tested using Socket.Connect()) I've boiled the code down to resemble the examples for using both of these methods presented in the Microsoft documentation.

Connection = new TcpClient(LocalEndPoint);  
Connection.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);  
Connection.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);  

Any help provided to resolve this issue would be much appreciated.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,375 questions
Windows Network
Windows Network
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Network: A group of devices that communicate either wirelessly or via a physical connection.
665 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce ( 57,886 Reputation points

    so does telnet to work?

    0 comments No comments

  2. Nathan Lyon 1 Reputation point

    Yes it does, and I've stumbled upon the fix since I posted the question. Long and frustrating story short, I was binding the tcp client to IpAddress.Loopback instead of IpAddress.Any. This is why it was working in my local tests and not in my dev environment tests. Sometimes you're just too close to your own code to see the mistakes.

    0 comments No comments

  3. Limitless Technology 44,001 Reputation points

    Hi. Thank you for your question and reaching out.

    Try resetting the TCP/IP Stack.

    STEP 1. Access the command window as an administrator.
    STEP 2. We will now run a number of commands in the command window.
    STEP 3. To test the TCP/IP configuration, use the first command.
    netsh int ip reset

    STEP 2. Using the second command, the present IP configuration will be removed.
    ipconfig /release

    STEP 3. An IP address will be reset using the third instruction.
    ipconfig /renew

    STEP 4. The faulty or erroneous DNS cache will be cleared by the fourth command.
    ipconfig /flushdns

    STEP 5. Finally, the primary command to repair any socket issues in the system
    netsh winsock reset

    STEP 6. Any network troubles with your system will be fully fixed by the following set of commands.
    STEP 7. Your computer should be restarted to see whether the error still occurs.


    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments