learning... learning... - 1 month ago 11
Vb.net Question

DNS.GetHostEntry giving the wrong information

When i pass an ip to do

Dns.GetHostEntry(ServerTextBox.Text)
, it gives me a totaly wrong IP back. When i use this ip to connect to my server, it fails with a message as "connection actively refused by the server".

This method is receiving IP: ###.###.##.119 but giving back serverAddress as ###.###.##.56. In the AddressList i am only seeing one ip which is .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


Due to the wrong ip, then the following code throws an exception. I had to hard code the ip back here again to make the connection.

'hard coded ip
_ServerAddress = System.Net.IPAddress.Parse("###.###.##.119")
_Connection = New ConnectionInfo(_ServerAddress, CInt(PortTextBox.Text), AddressOf InvokeAppendOutput)


What am i missing here?

Answer

There is no such thing as a wrong IP address. One DNS entry can have many IP addresses that point to the same host, on the same or on different interfaces.

Then a socket can be listening on the "any address" (0.0.0.0), or on specific addresses. If it's listening on a specific IP address and you try to connect to another one, even if it points to the same host, it'll refuse the connection.

There could also be a firewall that blocks request from a certain subnet to a certain address, when at the same time from another subnet you can connect just fine.

There's no way to know from a DNS query which address you should use. You could try to find out whether there's an address on the same subnet as the one you're connecting from. This might improve your chances of successfully connecting, but it's no guarantee.