【问题标题】:Get right ip adress of pc with multiple network cards使用多个网卡获取PC的正确IP地址
【发布时间】:2011-02-28 14:38:30
【问题描述】:

大家好, 我有一个侦听套接字的应用程序。 问题是 pc 有 2 个网卡并连接到公司 netork 和 plc 网络,当然我们必须监听/绑定/...到我们从公司网络中的 DHCP 获得的 IPAdress。

但是当我们这样做时:

System.Net.IPEndPoint(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0)

我们得到了 PLC 网络的 IP。现在我们正在寻找一种动态查找正确IPAdress 的方法。 我已经得到提示,您可以将套接字绑定到 IPAdress (0:0:0:0),但我们认为这样做有点冒险。

有没有人有一些想法来解决这个问题或关于 0:0:0:0 的一些评论?

提前致谢。

乔纳森

【问题讨论】:

  • 什么操作系统?编程语言?请适当标记。

标签: sockets networking interface hostname


【解决方案1】:

绑定到 0.0.0.0 或将其保留为默认值的唯一风险是您将接受来自两个网络的连接。只有您知道那是否存在风险,即其他网络中是否有您不想连接到您的东西。绑定到 0.0.0.0,也就是 INADDR_ANY,是网络编程中的默认和近乎普遍的做法。

【讨论】:

  • hej,感谢您的回复。 @tobias:我们正在考虑这个问题,但我们想要更多通用解决方案,而不是链接到特定范围的 IP 地址。 @EJP:我们不知道绑定到 0.0.0.0 的范围如此广泛。
  • Hej 我将代码更改为 New IpEndpoint(IPAddress.Any, Port) 但在创建套接字时出现异常“请求的地址在其上下文 0.0.0.0 中无效:”端口“有有人有什么想法吗?
  • 如果这是文字错误消息,您的代码中必须有“端口”的引号。
【解决方案2】:

您不能遍历所有地址并使用不是 0.* 和 168.* 的地址(或 dhcp 提供的任何地址...)

在大多数(!)情况下应该这样做。

【讨论】:

    【解决方案3】:

    我让用户决定他/她想要连接到哪个网络接口并将其放置在 AppSetting 中。 然后我创建一个模块来读取配置文件来决定要连接到哪个网络接口,并检查并获取我使用此代码的 IPAddress

    在 vb.net 中:

     Dim networkinterfaces() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
                Dim found As Boolean = False
    
                For Each ni As NetworkInterface In networkinterfaces
                    If NetworkInterfaceName.Equals(ni.Name) Then
                        If ni.GetPhysicalAddress().ToString().Length > 0 Then
                            IPAddressFromNetworkCard = ni.GetIPProperties.UnicastAddresses(0).Address
                            found = True
                            Exit For
                        End If
                    End If
                Next
    

    在 c# 中(更多跟踪,但几乎相同):

    Console.WriteLine("Test get ip of interfacecard");
                Console.WriteLine("Give name of interfacecard:");
                string s = Console.ReadLine();
    
    
                List<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().ToList<NetworkInterface>();
                Console.WriteLine(nics.Count + "  networkinterfaces found");
    
                bool found = false;
                foreach (NetworkInterface ni in nics)
                {
                    Console.WriteLine("Available nic: " + ni.Name);
                }
                Console.WriteLine("");
                Console.WriteLine(String.Format("searching for: \"{0}\"", s));
                foreach (NetworkInterface ni in nics)
                {
                    if (ni.Name.Equals(s))
                    {
                        if (ni.GetPhysicalAddress().ToString().Length > 0)
                        {
                            Console.WriteLine("Network interface found, ipAddress: " + ni.GetIPProperties().UnicastAddresses[0].Address.ToString());
                            found = true;
                            break;
                        }
                    }
    
                }
                if (!found)
                    Console.WriteLine(String.Format("\"{0}\" not found", s));
    
                Console.ReadKey();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2014-11-26
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多