背景:我的程序启动,并且必须获取正在其上运行的计算机的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)
3 回答
catspeake
TA贡献1111条经验 获得超0个赞
我假设特定的客户端计算机比收到此错误的网络有些故障。
引用@Stephen Cleary
此错误表明网络未连接或配置不正确。这绝对是客户端计算机上的错误,而不是服务器上的错误。您无法做很多事情来“解决”问题。您几乎可以做的就是升级客户端的网络驱动程序并检查连接问题(也许它们几乎不在无线范围内,或者以太网电缆缺少其锁定选项卡)。
慕慕森
TA贡献1856条经验 获得超17个赞
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() : "";
- 3 回答
- 0 关注
- 172 浏览
添加回答
举报
0/150
提交
取消