Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetHostEntry() doesn't resolve the address anymore

Tags:

c#

I recently switched from .NET v3.5 to v4.0 Client Profile and at first run GetHostEntry() got problem.

          tcpClient.SocketInfo.SourceName = remoteMatcher.Host; // "88.255.126.48"
          tcpClient.SocketInfo.SourcePort = remoteMatcher.Port; // 999

          IPHostEntry ipEntry = Dns.GetHostEntry(tcpClient.SocketInfo.SourceName);

GetHostEntry() causes an exception:

WSANO_DATA 11004 Valid name, no data record of requested type. The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable.

I'm gonna reboot the machine and wanted to ask this question before all things lost on my mind.

UPDATE:

My workaround:

   // .NET Framework v4.0 bug?? 
   IPAddress ip;
   if (IPAddress.TryParse(tcpClient.SocketInfo.SourceName, out ip))
       tcpClient.SocketInfo.SourceIP = tcpClient.SocketInfo.SourceName;
   else
   {
       IPHostEntry ipEntry = Dns.GetHostEntry(tcpClient.SocketInfo.SourceName);
       IPAddress[] addr = ipEntry.AddressList;
       tcpClient.SocketInfo.SourceIP = addr[addr.Length - 1].ToString();
   }
like image 865
Nime Cloud Avatar asked Jul 28 '11 09:07

Nime Cloud


2 Answers

here is something that I have tried, I recall running into the same problem feel free to use my example to test your stuff

** I used IPHostEntry instead **

string[] host = (address.Split('@'));
string hostname = host[1];

IPHostEntry IPhst = Dns.Resolve(hostname);
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
Socket s= new Socket(endPt.AddressFamily, 
SocketType.Stream,ProtocolType.Tcp);
s.Connect(endPt);

or when I used it to get hostname of email address

        try
        {
            Response.Write("One");
            string[] host = (txtEmailAddress.Text.Split('@'));
            string hostname = host[1];
            Response.Write(host);
            IPHostEntry IPhst = Dns.Resolve(hostname);
            IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
            Socket s = new Socket(endPt.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);
            Response.Write(endPt);
            s.Connect(endPt);
        }
        catch (SocketException se)
        {
            lblErrMsg.Text = se.Message.ToString();
            PublicUtils.AddError("Error: " + se.Message + txtEmailAddress.Text);
            txtEmailAddress.Focus();
            return;
        }  
like image 198
MethodMan Avatar answered Oct 19 '22 23:10

MethodMan


Encountered the same problem recently, GetHostEntry does a reverse lookup on host name when given an IP Address, in my particular scenario, NetBIOS on the target machine was turned off, that's why hostname resolution was failing, and GetHostEntry was throwing the above exception mentioned.

GetHostAddresses was more suitable for my needs, it does not do a reverse lookup when given an IP address, it just parses it and returns IPAddress itself.

From MSDN: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx

GetHostEntry method exhibits the following behavior when an IP string literal is passed:

  1. The method tries to parse the address. If the hostNameOrAddress contains a legal IP string literal, then the first phase succeeds.
  2. 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.
  3. 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.
like image 35
Faisal Mansoor Avatar answered Oct 19 '22 22:10

Faisal Mansoor