I want to check if some email domain exist.
When Dns.GetHostEntry(domain) throw an exception I know for sure that the domain doesn’t exist.
- Can I say that if
Dns.GetHostEntry(domain)succeeded then the domain does exists or even ifDns.GetHostEntry(domain)succeeded that doesn’t mean (yet) that domain exists? - Can I say the same when
s.Connectfails to connect? I mean if connect throw an exception can I say that such domain doesn’t exist?
If (1) is true, so in order to check if domain exists (1) will be enough, right?
public static bool Lookup(string domain)
{
if (domain == null) throw new ArgumentNullException("domain");
try {
IPHostEntry ipHost = Dns.GetHostEntry(domain);
var endPoint = new IPEndPoint(ipHost.AddressList[0], _dnsPort);
return Transfer(endPoint);
}
catch (SocketException ex)
{
++attempts;
}
return false;
}
public static bool Transfer(IPEndPoint endPoint)
{
int attempts = 0;
while(attempts <= _attempts)
{
try
{
var s = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(endPoint);
}
catch (SocketException ex)
{
++attempts;
}
finally
{
s.Close();
}
}
}
When trying to connect with a socket you’re saying a few things:
All of these occur after the hostname has been resolved to an IP.
So the answer to your question is no. If
Socket.Connectfails it could be that the domain doesn’t exist, or any of the above reasons (and perhaps more).