{"id":3807,"date":"2013-03-21T16:53:47","date_gmt":"2013-03-21T14:53:47","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=3807"},"modified":"2013-03-21T16:53:47","modified_gmt":"2013-03-21T14:53:47","slug":"how-to-shorten-connect-timeout","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/how-to-shorten-connect-timeout","title":{"rendered":"How to shorten Connect timeout"},"content":{"rendered":"<p><em>Connect<\/em> and <em>BeginConnect <\/em>methods take quite a long time to timeout, when you use incorrect server address. It usually takes almost 20 seconds for those methods to decide that connection is impossible. Setting SendTimeout\/ReceiveTimeout doesn&#8217;t influence this in any way.<\/p>\n<p>Not only <em>Imap<\/em>, <em>Pop3<\/em>, and <em>Smtp <\/em>classes suffer from this problem, it&#8217;s the same for regular <em>Socket <\/em>class.<\/p>\n<p>There is a solution however, using <em>BeginConnect <\/em>and simply waiting for specified amount of time (<em>AsyncWaitHandle.WaitOne<\/em>) without calling EndConnect (which blocks):<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing(Imap imap =new Imap())\r\n{\r\n    IAsyncResult result = imap.BeginConnectSSL(&quot;imap.example.com&quot;);\r\n\r\n    \/\/ 5 seconds timeout\r\n    bool success = result.AsyncWaitHandle.WaitOne(5000, true);\r\n\r\n    if (success == false)\r\n    {\r\n        throw new Exception(&quot;Failed to connect server.&quot;);\r\n    }\r\n    imap.EndConnect(result);\r\n\r\n    \/\/...\r\n\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nUsing imap As New Imap()\r\n\tDim result As IAsyncResult = imap.BeginConnectSSL(&quot;imap.example.com&quot;)\r\n\r\n\t' 5 seconds timeout\r\n\tDim success As Boolean = result.AsyncWaitHandle.WaitOne(5000, True)\r\n\r\n\tIf success = False Then\r\n\t\tThrow New Exception(&quot;Failed to connect server.&quot;)\r\n\tEnd If\r\n\timap.EndConnect(result)\r\n\r\n\t'...\r\n\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Connect and BeginConnect methods take quite a long time to timeout, when you use incorrect server address. It usually takes almost 20 seconds for those methods to decide that connection is impossible. Setting SendTimeout\/ReceiveTimeout doesn&#8217;t influence this in any way. Not only Imap, Pop3, and Smtp classes suffer from this problem, it&#8217;s the same for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[28,42,50],"class_list":["post-3807","post","type-post","status-publish","format-standard","hentry","category-mail-dll","category-programming","tag-imap","tag-pop3","tag-smtp"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3807"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=3807"}],"version-history":[{"count":4,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3807\/revisions"}],"predecessor-version":[{"id":3811,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3807\/revisions\/3811"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=3807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=3807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=3807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}