当我通过一个ip来做Dns.GetHostEntry(ServerTextBox.Text)
时,它会给我一个完全错误的IP。当我使用这个ip连接到我的服务器时,它会失败,因为有一条消息是“服务器主动拒绝连接”。
此方法是接收IP:###.###.##.119,但将serverAddress作为###.###.##.56返回。在AddressList中,我只看到一个ip,即.56。
_ServerAddress = Nothing
Dim remoteHost As IPHostEntry = Dns.GetHostEntry(ServerTextBox.Text)
If remoteHost IsNot Nothing AndAlso remoteHost.AddressList.Length > 0 Then
For Each deltaAddress As IPAddress In remoteHost.AddressList
If deltaAddress.AddressFamily = AddressFamily.InterNetwork Then
_ServerAddress = deltaAddress
Exit For
End If
Next
End If
由于ip错误,下面的代码将引发异常。我不得不在这里重新对ip进行硬编码才能建立连接。
'hard coded ip
_ServerAddress = System.Net.IPAddress.Parse("###.###.##.119")
_Connection = New ConnectionInfo(_ServerAddress, CInt(PortTextBox.Text), AddressOf InvokeAppendOutput)
我在这里错过了什么?
发布于 2016-11-02 21:26:36
没有错误的IP地址。一个DNS条目可以有多个指向同一主机、相同或不同接口的IP地址。
然后,套接字可以监听“任意地址”(0.0.0.0)或特定地址。如果它正在监听特定的IP地址,并且试图连接到另一个IP地址,即使它指向同一台主机,它也会拒绝连接。
防火墙也可以阻止从某个子网到某个地址的请求,而同时从另一个子网可以很好地连接。
从DNS查询中无法知道您应该使用哪个地址。您可以尝试找出与您连接的子网相同的子网是否有地址。这可能会提高您成功连接的机会,但这并不能保证。
https://stackoverflow.com/questions/40390034
复制相似问题