获取具有多个网卡的电脑的正确IP地址
大家好, 我有一个侦听套接字的应用程序。 问题是电脑有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绑定到 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.
你不能循环遍历所有地址并使用不是 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.
我让用户决定他/她想要连接到哪个网络接口并将其放置在 AppSetting 中。
然后我创建一个模块,该模块读取配置文件来决定连接到哪个网络接口,并检查并获取 IP 地址,我
在 vb.net:
in c# 中使用此代码(更多跟踪,但效果几乎相同):
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:
in c# (some more tracing, but it almost does the same):