GetHostEntry() doesn't resolve the address anymore
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 ret开发者_Go百科urned 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();
}
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;
}
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:
- The method tries to parse the address. If the hostNameOrAddress 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.
精彩评论