简体   繁体   English

为什么获取IP地址会引发异常?

[英]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. 背景:我的程序启动,它必须获取运行它的机器的IP地址。

It is the "server" in a client-server architecture that receives incoming tcp-ip messages. 客户机-服务器体系结构中的“服务器”接收传入的tcp-ip消息。

I should also add the machine : 我还应该添加机器:

  • Has multi-ip addresses available 有可用的多IP地址

  • Is running Windows 2008 R2 Server 正在运行Windows 2008 R2 Server

Here is the code that obtains the IP address: 这是获取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();
            }
        }
    }

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 解决方案:我已将代码更改为Rodcesar.santos在此建议的内容: 获取本地IP地址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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