<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; SOL_SOCKET</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/sol_socket/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>非阻塞connect的一个细节</title>
		<link>http://blog.yufeng.info/archives/565</link>
		<comments>http://blog.yufeng.info/archives/565#comments</comments>
		<pubDate>Tue, 18 May 2010 05:04:02 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[网络编程]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[SOL_SOCKET]]></category>
		<category><![CDATA[SO_ERROR]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=565</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: 非阻塞connect的一个细节 昨天听zhuzhaoyuan说的一个connect细节. 通常我们connect的时候都是非阻塞的, 在connect调用后把句柄挂到poll去, 等poll通知可写的时候, 我们就认为connect成功了. 但是在linux平台下实际上不一定成功, 具体的要用socket get_opt来检查下出错码来决定. 以下是从man 2 connnect摘抄的: EINPROGRESS The socket(2,7,n) is non-blocking and the connection cannot be com- pleted immediately. It is possible to select(2,7,2 select_tut)(2) or poll(2) for completion by selecting the socket(2,7,n) for writing. After select(2,7,2 select_tut) indicates writability, use getsockopt(2) to read(2,n,1 builtins) the [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/565">非阻塞connect的一个细节</a></p>
</div>
<p>昨天听zhuzhaoyuan说的一个connect细节. 通常我们connect的时候都是非阻塞的, 在connect调用后把句柄挂到poll去, 等poll通知可写的时候, 我们就认为connect成功了. 但是在linux平台下实际上不一定成功, 具体的要用socket get_opt来检查下出错码来决定.</p>
<p>以下是从man 2 connnect摘抄的:</p>
<blockquote><p> EINPROGRESS<br />
              The socket(2,7,n) is non-blocking and the  connection  cannot  be  com-<br />
              pleted  immediately.  It is possible to select(2,7,2 select_tut)(2) or poll(2) for<br />
              completion by selecting the socket(2,7,n)  for  writing.  After  select(2,7,2 select_tut)<br />
              indicates  writability,  use  getsockopt(2) to read(2,n,1 builtins) the SO_ERROR<br />
              option at level SOL_SOCKET to  determine  whether  connect  com-<br />
              pleted   successfully   (SO_ERROR  is  zero)  or  unsuccessfully<br />
              (SO_ERROR is one of the usual error(8,n) codes listed here,  explain-<br />
              ing the reason for the failure).</p></blockquote>
<p>我们看下erlang的inet_drv是如何处理的.<br />
./erts/emulator/drivers/common/inet_drv.c:</p>
<pre class="brush: cpp; title: ; notranslate">
        {
            int error = 0;      /* Has to be initiated, we check it */
            unsigned int sz = sizeof(error); /* even if we get -1 */
            int code = sock_getopt(desc-&gt;inet.s, SOL_SOCKET, SO_ERROR,
                                   (void *)&amp;error, &amp;sz);

            if ((code &lt; 0) || error) {
                desc-&gt;inet.state = TCP_STATE_BOUND;  /* restore state */
                ret = async_error(INETP(desc), error);
                goto done;
            }
        }
</pre>
<p>结论: 细节决定品质.
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/565/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

