为什么获取IP地址会抛出异常?

时间:2018-04-12 18:08:18

标签: c# windows tcp-ip

背景: 我的程序启动,它必须获取正在运行的机器的IP地址。

这是"服务器"在接收传入tcp-ip消息的客户端 - 服务器体系结构中。

我还应该添加机器:

  • 有多个IP地址可用

  • 正在运行Windows 2008 R2 Server

以下是获取IP地址的代码:

    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();
            }
        }
    }

以下是错误消息:

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

解: 我已将我的代码更改为rodcesar.santos在此建议的内容: Get local IP address

这是我的(修改过的)代码(并且有效)

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

2 个答案:

答案 0 :(得分:1)

阅读接受的答案@ A socket operation was attempted to an unreachable host

我认为特定的客户端计算机比收到此错误的网络有点错误。

引用@Stephen Cleary

  

此错误表示网络未连接或未正确配置。这绝对是客户端计算机上的错误,而不是您的服务器。   你无法做很多事情来解决"问题。您可以做的就是升级客户端的网络驱动程序并检查连接问题(可能它们几乎不在无线范围内,或者以太网电缆缺少其锁定选项卡)。

答案 1 :(得分: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() : "";