{"id":809,"date":"2010-04-12T18:03:29","date_gmt":"2010-04-12T16:03:29","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=809"},"modified":"2010-04-12T18:03:29","modified_gmt":"2010-04-12T16:03:29","slug":"a-non-blocking-socket-operation","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/a-non-blocking-socket-operation","title":{"rendered":"A non-blocking socket operation could not be completed"},"content":{"rendered":"<p>There is a bug in .NET 2.0 regarding timeout handling by Socket class.<br \/>\nHere&#8217;s the code that reproduces this behavior.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing(TcpClient client = new TcpClient())\n{\n    \/\/ Set timeout\n    client.ReceiveTimeout =\n        (int)TimeSpan.FromSeconds(1).TotalMilliseconds;\n\n    \/\/ Connect to perfectly working imap server\n    client.Connect(&quot;mail.limilabs.com&quot;, 143);\n\n    \/\/ Create reader and writer\n    NetworkStream stream = client.GetStream();\n    StreamReader reader = new StreamReader(stream);\n    StreamWriter writer = new StreamWriter(stream);\n\n    \/\/ Read hello line\n    Console.WriteLine(reader.ReadLine());\n\n    \/\/ this works with no problems\n    writer.WriteLine(&quot;a NOOP&quot;);             \/\/ No operation command\n    writer.Flush();\n    Console.WriteLine(reader.ReadLine());   \/\/ No operation response\n\n    try\n    {\n        reader.ReadLine();                  \/\/ Nothing to read\n    }\n    catch (Exception ex)                    \/\/ Timeout\n    {\n        Console.WriteLine(&quot;timeout&quot;);       \/\/ This is expected\n    }\n\n    writer.WriteLine(&quot;b NOOP&quot;);             \/\/ No operation command\n    writer.Flush();\n\n    \/\/ SocketException: A non-blocking socket operation\n    \/\/ could not be completed immediately\n    Console.WriteLine(reader.ReadLine());\n\n    client.Close();\n}\n\n<\/pre>\n<p>The above code runs as expected on Mono and on .NET 4.0.<\/p>\n<p>The problem is in UpdateStatusAfterSocketError internal Socket method that executes SetToDisconnected method when any exception is thrown, including timeout exception.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a bug in .NET 2.0 regarding timeout handling by Socket class. Here&#8217;s the code that reproduces this behavior. using(TcpClient client = new TcpClient()) { \/\/ Set timeout client.ReceiveTimeout = (int)TimeSpan.FromSeconds(1).TotalMilliseconds; \/\/ Connect to perfectly working imap server client.Connect(&quot;mail.limilabs.com&quot;, 143); \/\/ Create reader and writer NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[9,35],"class_list":["post-809","post","type-post","status-publish","format-standard","hentry","category-programming","tag-net","tag-mono"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/809"}],"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=809"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/809\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}