<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 24 September 2017 at 19:37, Vinnie Falco via Boost-users <span dir="ltr"><<a href="mailto:boost-users@lists.boost.org" target="_blank">boost-users@lists.boost.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">>On Sun, Sep 24, 2017 at 1:15 AM, martin doc via Boost-users <<a href="mailto:boost-users@lists.boost.org">boost-users@lists.boost.org</a>> wrote:<br> > As boost::asio::connect() closes an open socket passed to it before making<br> > the remote connection, it's not possible to pass connect() a prebaked<br> > socket.<br> <br> </span>What makes you think that? My reading of the implementation is that<br> the socket is only closed when:<br> <br> 1. The range of addresses passed to connect() has more than one element, and<br> 2. The connection attempt for the first address fails.<br> <br> See:<br> <br> <<a href="https://github.com/boostorg/asio/blob/b002097359f246b7b1478775251dfb153ab3ff4b/include/boost/asio/impl/connect.hpp#L108" rel="noreferrer" target="_blank">https://github.com/boostorg/<wbr>asio/blob/<wbr>b002097359f246b7b1478775251dfb<wbr>153ab3ff4b/include/boost/asio/<wbr>impl/connect.hpp#L108</a><br> <br> You can avoid the socket closure by only attempting to connect to one address.<br></blockquote><div><br></div><div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default">Judging from that code, the socket is always closed before attempting a connect. The `s.close()` is right above the `s.connect()`.<br></div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default"><br></div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default">My guess is this is because the iterated endpoints may be using different protocols (TCP/IPv4 or TCP/IPv6 for example). Things become a bit hairy when you take that into account. You can't use a local TCP/IPv4 endpoint and connect to a TCP/IPv6 peer. What should `asio::connect` do in that case? Ignore the IPv6 endpoint? Or just not bind to a specific local endpoint? Or should you give it an iterator with at least one local endpoint per protocol in the iterated remote endpoints? And in that case, what should `asio::connect` do if there isn't a matching local endpoint? Return an error? Or not bind to a local endpoint? Personally I'm not sure which behaviour makes the most sense here. It seems like ASIO decided to sidestep the whole issue.<br></div></div><div><br></div><div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default">You can still use `socket.connect()` directly with a single endpoint. Alternatively, you can wrap an already connected socket by passing the file descriptor to asio:</div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default"><br></div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default"><a href="http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/basic_stream_socket/basic_stream_socket/overload4.html">http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/basic_stream_socket/basic_stream_socket/overload4.html</a></div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default"><a href="http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/basic_stream_socket/assign.html">http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/basic_stream_socket/assign.html</a><br></div></div><div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default"></div><div style="font-family:arial,helvetica,sans-serif" class="gmail_default">Another option is to write your own `connect` function that does support binding to a local endpoint. If you have a nice way of dealing with different protocols in the iterated remote endpoints, you could even suggest your function for inclusion in ASIO.<br></div></div><div> </div>--<div style="font-family:arial,helvetica,sans-serif;display:inline" class="gmail_default"> Maarten</div><br></div></div></div>