简体   繁体   中英

Why does getting an IP address throw an exception?

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() : "";

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).

            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() : "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM