获取机器上的所有IP地址
如何获取连接到我的应用程序(C# NET 控制台应用程序)正在运行的计算机的所有 IP 地址?我需要将 WCF 服务绑定到主 IP 地址,并返回完整 IP 地址列表的列表。
using System.Net;
string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();
这就是我现在用来获取主 IP 地址的方法,但我不知道如何让其余的地址返回它们。
如果我将 WCF 服务绑定到 localhost:8000,是否会将其暴露在主服务器上?
How can I get all of the IP addresses attached to the machine that my application (C# NET Console app) is running on? I need to bind a WCF service to the primary IP address, and return a list of the full IP address list.
using System.Net;
string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();
This is what I am using right now to get the Primary IP address, but I can't figure out how to get the rest to return them.
If I bind a WCF service to localhost:8000, will that expose it on the primary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
DNS 变体在整个网络中工作,但一个 DNS 条目可以有多个 IP 地址,而一个 IP 地址可以有多个 DNS 条目。
更重要的是,地址根本不需要绑定到 DNS 条目。
对于本地机器,请尝试以下操作:
The DNS variants work across the network, but one DNS entry can have many IP addresses and one IP address can have many DNS entries.
More importantly, an address needn't be bound to a DNS entry at all.
For the local machine try this:
我认为这个例子应该对你有帮助。
编辑:
“不存在“主”IP 地址之类的东西。
路由表根据目标 IP 地址(以及扩展的网络接口,其本身可以是虚拟的或物理的)确定使用哪个向外的 IP 地址。 ”。
I think this example should help you.
Edit:
"There's no such thing as a "primary" IP address.
The routing table determines which outward-facing IP address is used depending on the destination IP address (and by extension, the network interface, which itself can be virtual or physical)."
为什么不直接绑定到 0.0.0.0 ?
这样你就可以监听所有的ip
Why not just bind to 0.0.0.0 ?
That way you listen on all ips
您可能应该绑定到
0.0.0.0:8000
,这会将其公开在所有可用的 IP 地址上,并且仅在用户/管理员要求时才绑定到特定的 IP 地址。You should probably bind to
0.0.0.0:8000
, that will expose it on all available IP addresses and only bind to a particular IP address if the user/administrator demands so.我认为 OP 正在询问如何获取本地 NIC 上的所有地址,而不仅仅是 DNS 已知的那些地址。
通过“主要”,他可能指的是适配器属性中“使用以下 IP 地址”下的主地址,通过“其余”,他可能指的是“高级”>“其他”中列出的地址。 (附加)IP 地址。
DNS 不一定知道这些。
I think the OP is asking about how to get all addresses on a local NIC, not just those addresses known to DNS.
By primary he probably means the main address under "use the following IP address" in the adapter properties, and by "the rest" he probably means those listed in Advanced > (Additional) IP Addesses.
DNS will not necessarily know those.