0

Background: My program starts up and it must obtain the IP address of the machine it is running on.

It is the "server" in a client-server architecture that receives incoming tcp-ip messages.

I should also add the machine :

  • Has multi-ip addresses available

  • Is running Windows 2008 R2 Server

Here is the code that obtains the IP address:

    public bool IsNetworkAvailable
    {
        get
        {
            return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
        }
    }

    public string thisIP { get; private set; }
    public void GetThisIP()
    {
        if (!string.IsNullOrEmpty(thisIP))
        {
            return;
        }

        thisIP = "*";

        if (IsNetworkAvailable)
        {
            using (System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(
                System.Net.Sockets.AddressFamily.InterNetwork,
                System.Net.Sockets.SocketType.Dgram, 0))
            {
                socket.Connect("11.0.1.5", 65530);
                System.Net.IPEndPoint endPoint = socket.LocalEndPoint as System.Net.IPEndPoint;
                thisIP = endPoint.Address.ToString();
            }
        }
    }

Here is the error message:

(0x80004005): A socket operation was attempted to an unreachable network 11.0.1.5:65530 
at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)

SOLUTION: I have changed my code to what rodcesar.santos suggested in this: Get local IP address

here is my (modified) code (and it works)

            System.Net.NetworkInformation.UnicastIPAddressInformation mostSuitableIp = null;

            var networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (var network in networkInterfaces)
            {
                if (network.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                var properties = network.GetIPProperties();

                if (properties.GatewayAddresses.Count == 0)
                {
                    continue;
                }

                if (mostSuitableIp != null)
                {
                    break;
                }

                foreach (var address in properties.UnicastAddresses)
                {
                    if (address.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    if (System.Net.IPAddress.IsLoopback(address.Address))
                    {
                        continue;
                    }

                    if (mostSuitableIp == null && address.IsDnsEligible)
                    {
                        mostSuitableIp = address;
                        break;
                    }
               }
            }

            thisIP = mostSuitableIp != null ? mostSuitableIp.Address.ToString() : "";
MrLister
  • 634
  • 7
  • 32
  • If it works on all but one machine, it's likely a problem with the machine itself, not the code. – Daxtron2 Apr 12 '18 at 18:10
  • There's obviously an issue with the setup on that machine preventing the socket from opening. Can you [just do this](https://stackoverflow.com/a/6803109/3608792) instead? – Dan Wilson Apr 12 '18 at 18:10
  • Can you ping that IP from the machine? – Sergio Monteleone Apr 12 '18 at 18:10
  • Dan Wilson : from your link, there is another entry "There is a more accurate way when there are multi ip addresses available on local machine. Connect a UDP socket and read its local endpoint" ... this is why I am doing what I am doing... – MrLister Apr 12 '18 at 18:18
  • S. Monteleone - yes it pings just fine... (it's IP is 192.168.0.189 ... so it's within our local DNS ) – MrLister Apr 12 '18 at 18:20

2 Answers2

1

After reading the accepted answer @ A socket operation was attempted to an unreachable host,

I assume that the specific client-computer than receives this error has a somewhat faulty network.

Quoting @Stephen Cleary

This error indicates that the network was not connected or not configured correctly. It is definitely an error on the client machine, not your server. There isn't much you can do to "solve" the problem. Pretty much all you can do is upgrade the client's network drivers and check for connection problems (maybe they're barely within wireless range, or the Ethernet cable is missing its locking tab).

Bill Souvas
  • 46
  • 2
  • 6
  • I will check this out and research further... thanks – MrLister Apr 12 '18 at 18:21
  • @user3174075 - If this turns out to be the answer, please don't forget to accept it. – Brian Apr 12 '18 at 18:53
  • @user3174075, is it possible that the problem computer does not have access to IP addresses not on the local network, due to a network, firewall, or antivirus configuration? The hardcoded address in your sample code is not on the same subnet with the 192.168.0.189 address you gave in the comments. You also said the local computer has multiple addresses, are any of those on the same subnet as the hardcoded address? – Sam Skuce Apr 12 '18 at 19:33
  • no firewall, no anti-virus ... what is strange is the code works on other machines... – MrLister Apr 12 '18 at 19:54
0
            System.Net.NetworkInformation.UnicastIPAddressInformation mostSuitableIp = null;

            var networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (var network in networkInterfaces)
            {
                if (network.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                var properties = network.GetIPProperties();

                if (properties.GatewayAddresses.Count == 0)
                {
                    continue;
                }

                if (mostSuitableIp != null)
                {
                    break;
                }

                foreach (var address in properties.UnicastAddresses)
                {
                    if (address.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    if (System.Net.IPAddress.IsLoopback(address.Address))
                    {
                        continue;
                    }

                    if (mostSuitableIp == null && address.IsDnsEligible)
                    {
                        mostSuitableIp = address;
                        break;
                    }
               }
            }

            thisIP = mostSuitableIp != null ? mostSuitableIp.Address.ToString() : "";
MrLister
  • 634
  • 7
  • 32