获取具有多个网卡的电脑的正确IP地址

发布于 2024-10-19 17:15:44 字数 421 浏览 3 评论 0原文

大家好, 我有一个侦听套接字的应用程序。 问题是电脑有 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 的一些评论?

提前致谢。

乔纳森

Hej All,
I've an application that listens on a socket.
The problem is that the pc has 2 network cards and is connected to the company netork and a plc network of course we have to listen/bind/... onto the IPAdress we got from the DHCP in the company network.

But when we do this:

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

We get the IP of the PLC network. Now we are looking for a way to find dynamically the right IPAdress.
I got already the tip that you can bind a socket to the IPAdress (0:0:0:0) but we think it's a bit risky to do so.

Has anyone some ideas to solve this issue or some remarks about the 0:0:0:0?

Thanks in advance.

Jonathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素罗衫 2024-10-26 17:15:44

绑定到 0.0.0.0 或将其保留为默认值的唯一风险是您将接受通过两个网络的连接。只有您知道是否存在风险,即其他网络中是否存在您不想连接的内容。绑定到 0.0.0.0(又名 INADDR_ANY)是网络编程中的默认且近乎普遍的做法。

The only risk in binding to 0.0.0.0, or leaving it as the default, is that you will accept connections via both networks. Only you know whether that is a risk, i.e. whether there are things in the other network that you don't want connecting to you. Binding to 0.0.0.0, aka INADDR_ANY, is the default and near-universal practice in network programming.

╭⌒浅淡时光〆 2024-10-26 17:15:44

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

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

Can't you loop through all adresses and use the one which is NOT 0.* and 168.* (or whatever the dhcp delivers...)

That should do in most(!) cases.

差↓一点笑了 2024-10-26 17:15:44

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

在 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

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

I let the user decide to what networkinterface he/she wanted to connect to and placed that in an AppSetting.
Then I create a module that reads the config file to decide what networkinterface to connect to, and to check and get the IPAddress I use this code

in 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

in c# (some more tracing, but it almost does the same):

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文