21 .net c# ip wcf networking
如何获取连接到我的应用程序(C#NET控制台应用程序)运行的计算机的所有IP地址?我需要将WCF服务绑定到主IP地址,并返回完整IP地址列表的列表.
using System.Net;
string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();
Run Code Online (Sandbox Code Playgroud)
这就是我现在正在使用的获取主IP地址,但我无法弄清楚如何让其余的返回它们.
如果我将WCF服务绑定到localhost:8000,是否会在主服务器上公开它?
小智 44
DNS变体在网络上工作,但一个DNS条目可以有许多IP地址,一个IP地址可以有许多DNS条目.更重要的是,地址根本不需要绑定到DNS条目.
对于本地机器试试这个: -
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("Name: " + netInterface.Name);
Console.WriteLine("Description: " + netInterface.Description);
Console.WriteLine("Addresses: ");
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
{
Console.WriteLine(" " + addr.Address.ToString());
}
Console.WriteLine("");
}
Run Code Online (Sandbox Code Playgroud)
我想这个例子可以帮到你.
// Get host name
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
....
}
Run Code Online (Sandbox Code Playgroud)
编辑:
"没有"主要"IP地址这样的东西.
路由表根据目标IP地址(以及扩展名为网络接口,可以是虚拟的或物理的)确定使用哪个外向IP地址.