简体   繁体   English

C#-IMAP主机名上的TcpClient超时不存在

[英]C# - TcpClient Timeout on IMAP Hostname that doesnt exist

So I have a very basic script: 所以我有一个非常基本的脚本:

#region Get Hostname IP
IPAddress[] dnsRecords = Dns.GetHostAddresses(hostname);
string ipHostname = dnsRecords[0].ToString();
if (ipHostname.Length == 0) {
    responseCode = imapLoginResponse.Failed;
    return false;
}
#endregion
#region Try make a request to the Host, Port
try {
    _Connection = new TcpClient(ipHostname, port);
} catch (SocketException ex) {
    System.Diagnostics.Debug.Write("-" + hostname + "-" + ipHostname + "- " + ex.Message);
    responseCode = ex.Message == "No such host is known" || ex.Message.StartsWith("No connection could be made because the target machine actively refused it") ? imapLoginResponse.BadHostname : imapLoginResponse.Failed;
    return false;
}

The code above simply tries to connect to an IMAP Server. 上面的代码只是尝试连接到IMAP服务器。

  • Get imap.{domain.ext} DNS Record IP 获取imap。{domain.ext} DNS记录IP
  • Try connect to that IP with port 993 尝试使用端口993连接到该IP
  • Voila 沃伊拉

What's happening for either dead/bad/abandoned/parked websites is: 死/坏/遗弃/停放的网站正在发生的情况是:

  • Get imap.{domain.ext} DNS Record IP, somehow will get one even though it doesn't exist 获取imap。{domain.ext} DNS记录IP,即使不存在,它也会以某种方式获得一个
  • Try to connect to that IP with port 993 尝试通过端口993连接到该IP
  • Server doesn't respond resulting in a timeout 服务器无响应导致超时

Now what do I do? 现在我该怎么办? I cant turn this down to an "BadHostname" as it could just be a connection issue on the user's end or even not connected to internet. 我不能将其拒绝为“ BadHostname”,因为它可能只是用户端的连接问题,甚至没有连接到Internet。

If I were to just retry here, it will end up infinitely looping. 如果我只是在这里重试,它将最终无限循环。 While yes, I could do a 5 retries == invalid thing, but that's not accurate and could still be a temporary internet issue. 是的,我可以进行5次重试==无效的操作,但这并不准确,仍然可能是暂时的互联网问题。

My question is, why is it getting a DNS Record IP for a record that doesnt exist? 我的问题是,为什么会为不存在的记录获取DNS记录IP? And what am I meant to do when something like this occurs? 当发生这种情况时我该怎么办?

If you need to test, try it with this: imap.celerityinc.com (no celebrityinc.com) and you can see for yourself. 如果您需要测试,请尝试以下操作:imap.celerityinc.com(无celebrityinc.com),您可以自己查看。

Name resolution and services provided on an IP address are independent. IP地址上的名称解析和服务是独立的。 You get a DNS record because it does exist at some point in the DNS hierarchy with a certain lifetime. 您会得到一条DNS记录,因为它确实存在于DNS层次结构中具有特定生存期的某个点上。

What you can do when something like this occurs? 发生这种情况时该怎么办? Flush your local DNS cache. 刷新本地DNS缓存。 Try different DNS servers. 尝试使用其他DNS服务器。 Go back to the SOA with your own DNS query solution. 使用您自己的DNS查询解决方案返回SOA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM