Boost C++ Libraries: Ticket #5157: Assign native socket problem https://svn.boost.org/trac10/ticket/5157 <p> Hi </p> <p> I'm trying to duplicate accepted/opened socket and pass them to other process. It works fine under linux. But under Windows I can't assign this native socket to boost::asio::ip::tcp::socket. When I try to assign opened native socket to boost socket object s.assign(boost::asio::ip::tcp::v4(), socketDup) throws me exception: "The parameter is incorrect". Even if I try to duplicate socket and use them in the same process. I still got this exception. </p> <p> The line inside boost throws this exception: boost_1_45_0\boost\asio\detail\impl\win_iocp_io_service.ipp: </p> <pre class="wiki">boost::system::error_code win_iocp_io_service::register_handle( HANDLE handle, boost::system::error_code&amp; ec) { if (::CreateIoCompletionPort(handle, iocp_.handle, 0, 0) == 0) //this function failed { DWORD last_error = ::GetLastError(); ec = boost::system::error_code(last_error, boost::asio::error::get_system_category()); } else { ec = boost::system::error_code(); } return ec; } </pre><p> I'm using: VC++ 2008 / Win7 </p> <p> What am I doing wrong? How can I resolve this problem? Thanks for help. </p> <p> Example code: </p> <pre class="wiki">boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort)); std::cout &lt;&lt; "Waiting for connection..." &lt;&lt; std::endl; tcp::socket socket(io_service); acceptor.accept(socket); std::cout &lt;&lt; "connection accepted" &lt;&lt; std::endl; #ifdef _WIN32 WSAPROTOCOL_INFO pi; WSADuplicateSocket(socket.native(), get_current_process_id(), &amp;pi); SOCKET socketDup = WSASocket(pi.iAddressFamily/*AF_INET*/, pi.iSocketType/*SOCK_STREAM*/, pi.iProtocol/*IPPROTO_TCP*/, &amp;pi, 0, 0); char sText[] = "I can use my duplicated socket via WinApi!\r\n"; int nRet = send(socketDup, sText, strlen(sText), 0); #else //linux int socketDup = dup(socket.native()); #endif try { tcp::socket s(io_service); s.assign(boost::asio::ip::tcp::v4(), socketDup); //this throws exception under Windows //I can't use my socket via boost lib s.send(boost::asio::buffer("Not work\r\n")); } catch(std::exception &amp;e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; //"The parameter is incorrect" exception } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5157 Trac 1.4.3 Ole Jak (aka Blender) Thu, 17 Mar 2011 17:05:58 GMT <link>https://svn.boost.org/trac10/ticket/5157#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5157#comment:1</guid> <description> <p> Hello my friends. </p> <p> I loved this peace of code. I decided to help us all opensource programmers a bit and try to solve this problem. </p> <p> I fixed code a bit and made it compilable. </p> <p> I received exception described. </p> <p> I <a class="ext-link" href="http://stackoverflow.com/questions/5326564/c-boost-asio-passing-accepted-tcp-connection-from-one-opened-socket-to-another"><span class="icon">​</span>addressed this question</a> to community of Grate <a class="ext-link" href="http://stackoverflow.com/"><span class="icon">​</span>StackOverflow</a> programmers. </p> <p> <a class="ext-link" href="http://stackoverflow.com/users/103167/ben-voigt"><span class="icon">​</span>Ben Voigt</a> *(it seems a member of msmvps)<a class="ext-link" href="http://stackoverflow.com/questions/5326564/c-boost-asio-passing-accepted-tcp-connection-from-one-opened-socket-to-another/5329413#5329413"><span class="icon">​</span>provided a solution</a> to this problem which was tested and that works. </p> <p> here is final code I compiled and tested: </p> <div class="wiki-code"><div class="code"><pre> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#ifdef _WIN32</span> <span class="cp">#define BOOST_ASIO_DISABLE_IOCP 1</span> <span class="cp">#endif</span> <span class="cp">#include</span> <span class="cpf">&lt;boost/asio.hpp&gt;</span><span class="cp"></span> <span class="cp">#ifdef _WIN32</span> <span class="cp">#include</span> <span class="cpf">&quot;Windows.h&quot;</span><span class="cp"></span> <span class="cp">#endif</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span> <span class="kt">int</span> <span class="n">m_nPort</span> <span class="o">=</span> <span class="mi">12345</span><span class="p">;</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">io_service</span> <span class="n">io_service</span><span class="p">;</span> <span class="n">tcp</span><span class="o">::</span><span class="n">acceptor</span> <span class="n">acceptor</span><span class="p">(</span><span class="n">io_service</span><span class="p">,</span> <span class="n">tcp</span><span class="o">::</span><span class="n">endpoint</span><span class="p">(</span><span class="n">tcp</span><span class="o">::</span><span class="n">v4</span><span class="p">(),</span> <span class="n">m_nPort</span><span class="p">));</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Waiting for connection...&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="n">tcp</span><span class="o">::</span><span class="n">socket</span> <span class="n">socket</span><span class="p">(</span><span class="n">io_service</span><span class="p">);</span> <span class="n">acceptor</span><span class="p">.</span><span class="n">accept</span><span class="p">(</span><span class="n">socket</span><span class="p">);</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;connection accepted&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="cp">#ifdef _WIN32</span> <span class="n">WSAPROTOCOL_INFO</span> <span class="n">pi</span><span class="p">;</span> <span class="n">WSADuplicateSocket</span><span class="p">(</span><span class="n">socket</span><span class="p">.</span><span class="n">native</span><span class="p">(),</span> <span class="n">GetCurrentProcessId</span><span class="p">(),</span> <span class="o">&amp;</span><span class="n">pi</span><span class="p">);</span> <span class="n">SOCKET</span> <span class="n">socketDup</span> <span class="o">=</span> <span class="n">WSASocket</span><span class="p">(</span><span class="n">pi</span><span class="p">.</span><span class="n">iAddressFamily</span><span class="cm">/*AF_INET*/</span><span class="p">,</span> <span class="n">pi</span><span class="p">.</span><span class="n">iSocketType</span><span class="cm">/*SOCK_STREAM*/</span><span class="p">,</span> <span class="n">pi</span><span class="p">.</span><span class="n">iProtocol</span><span class="cm">/*IPPROTO_TCP*/</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pi</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="kt">char</span> <span class="n">sText</span><span class="p">[]</span> <span class="o">=</span> <span class="s">&quot;I can use my duplicated socket via WinApi!</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="kt">int</span> <span class="n">nRet</span> <span class="o">=</span> <span class="n">send</span><span class="p">(</span><span class="n">socketDup</span><span class="p">,</span> <span class="n">sText</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">sText</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span> <span class="cp">#else</span> <span class="c1">//linux</span> <span class="kt">int</span> <span class="n">socketDup</span> <span class="o">=</span> <span class="n">dup</span><span class="p">(</span><span class="n">socket</span><span class="p">.</span><span class="n">native</span><span class="p">());</span> <span class="c1">// tested on Linux, works!</span> <span class="cp">#endif</span> <span class="k">try</span> <span class="p">{</span> <span class="n">tcp</span><span class="o">::</span><span class="n">socket</span> <span class="n">s</span><span class="p">(</span><span class="n">io_service</span><span class="p">);</span> <span class="n">s</span><span class="p">.</span><span class="n">assign</span><span class="p">(</span><span class="n">tcp</span><span class="o">::</span><span class="n">v4</span><span class="p">(),</span> <span class="n">socketDup</span><span class="p">);</span> <span class="c1">//this throws exception under Windows</span> <span class="c1">//I can&#39;t use my socket via boost lib</span> <span class="n">s</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">asio</span><span class="o">::</span><span class="n">buffer</span><span class="p">(</span><span class="s">&quot;Not work</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">));</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;We do not get here!=(&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="n">exception</span> <span class="o">&amp;</span><span class="n">e</span><span class="p">)</span> <span class="p">{</span> <span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="c1">//&quot;The parameter is incorrect&quot; exception</span> <span class="p">}</span> <span class="n">cin</span><span class="p">.</span><span class="n">get</span><span class="p">();</span> <span class="p">}</span> </pre></div></div><p> </p> </description> <category>Ticket</category> </item> </channel> </rss>