foreach 通过 DNS 主机地址
foreach over DNS host addresses
我有这个代码:
IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry("ip..");
foreach (IPAddress address in host.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 7777);
sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipe);
if (sock.Connected)
{
sock.SendTo(Encoding.UTF8.GetBytes("Hello world"), ipe);
}
}
此代码在本地主机上运行正常,但是当我写入 vps ip 时,代码无法运行,有什么问题吗?
您的 DNS 设置似乎不正确,Dns.GetHostEntry(string)
在下面的第二点失败。如果 DNS 服务器无法执行反向查找,它不会 return 主机名,因此 Dns.GetHostEntry(string)
不知道要查找什么,并且 return 将是一个空地址列表。
来自 MSDN:https://msdn.microsoft.com/en-us/library/ms143998.aspx
- The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
- A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
- The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.
我有这个代码:
IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry("ip..");
foreach (IPAddress address in host.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 7777);
sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipe);
if (sock.Connected)
{
sock.SendTo(Encoding.UTF8.GetBytes("Hello world"), ipe);
}
}
此代码在本地主机上运行正常,但是当我写入 vps ip 时,代码无法运行,有什么问题吗?
您的 DNS 设置似乎不正确,Dns.GetHostEntry(string)
在下面的第二点失败。如果 DNS 服务器无法执行反向查找,它不会 return 主机名,因此 Dns.GetHostEntry(string)
不知道要查找什么,并且 return 将是一个空地址列表。
来自 MSDN:https://msdn.microsoft.com/en-us/library/ms143998.aspx
- The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
- A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
- The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.