1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>async_connect (1 of 6 overloads)</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../async_connect.html" title="async_connect"> 9<link rel="prev" href="../async_connect.html" title="async_connect"> 10<link rel="next" href="overload2.html" title="async_connect (2 of 6 overloads)"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../async_connect.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_connect.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.reference.async_connect.overload1"></a><a class="link" href="overload1.html" title="async_connect (1 of 6 overloads)">async_connect 28 (1 of 6 overloads)</a> 29</h4></div></div></div> 30<p> 31 Asynchronously establishes a socket connection by trying each endpoint 32 in a sequence. 33 </p> 34<pre class="programlisting">template< 35 typename <a class="link" href="../Protocol.html" title="Protocol requirements">Protocol</a>, 36 typename <a class="link" href="../Executor1.html" title="Executor requirements">Executor</a>, 37 typename <a class="link" href="../EndpointSequence.html" title="Endpoint sequence requirements">EndpointSequence</a>, 38 typename <a class="link" href="../RangeConnectHandler.html" title="Range connect handler requirements">RangeConnectHandler</a> = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>> 39<a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.automatic_deduction_of_initiating_function_return_type"><span class="emphasis"><em>DEDUCED</em></span></a> async_connect( 40 basic_socket< Protocol, Executor > & s, 41 const EndpointSequence & endpoints, 42 RangeConnectHandler && handler = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>, 43 typename enable_if< is_endpoint_sequence< EndpointSequence >::value >::type * = 0); 44</pre> 45<p> 46 This function attempts to connect a socket to one of a sequence of endpoints. 47 It does this by repeated calls to the socket's <code class="computeroutput">async_connect</code> 48 member function, once for each endpoint in the sequence, until a connection 49 is successfully established. 50 </p> 51<h6> 52<a name="boost_asio.reference.async_connect.overload1.h0"></a> 53 <span class="phrase"><a name="boost_asio.reference.async_connect.overload1.parameters"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_connect.overload1.parameters">Parameters</a> 54 </h6> 55<div class="variablelist"> 56<p class="title"><b></b></p> 57<dl class="variablelist"> 58<dt><span class="term">s</span></dt> 59<dd><p> 60 The socket to be connected. If the socket is already open, it will 61 be closed. 62 </p></dd> 63<dt><span class="term">endpoints</span></dt> 64<dd><p> 65 A sequence of endpoints. 66 </p></dd> 67<dt><span class="term">handler</span></dt> 68<dd> 69<p> 70 The handler to be called when the connect operation completes. Copies 71 will be made of the handler as required. The function signature of 72 the handler must be: 73</p> 74<pre class="programlisting">void handler( 75 // Result of operation. if the sequence is empty, set to 76 // boost::asio::error::not_found. Otherwise, contains the 77 // error from the last connection attempt. 78 const boost::system::error_code& error, 79 80 // On success, the successfully connected endpoint. 81 // Otherwise, a default-constructed endpoint. 82 const typename Protocol::endpoint& endpoint 83); 84</pre> 85<p> 86 Regardless of whether the asynchronous operation completes immediately 87 or not, the handler will not be invoked from within this function. 88 On immediate completion, invocation of the handler will be performed 89 in a manner equivalent to using <a class="link" href="../post.html" title="post"><code class="computeroutput">post</code></a>. 90 </p> 91</dd> 92</dl> 93</div> 94<h6> 95<a name="boost_asio.reference.async_connect.overload1.h1"></a> 96 <span class="phrase"><a name="boost_asio.reference.async_connect.overload1.example"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_connect.overload1.example">Example</a> 97 </h6> 98<pre class="programlisting">tcp::resolver r(my_context); 99tcp::resolver::query q("host", "service"); 100tcp::socket s(my_context); 101 102// ... 103 104r.async_resolve(q, resolve_handler); 105 106// ... 107 108void resolve_handler( 109 const boost::system::error_code& ec, 110 tcp::resolver::results_type results) 111{ 112 if (!ec) 113 { 114 boost::asio::async_connect(s, results, connect_handler); 115 } 116} 117 118// ... 119 120void connect_handler( 121 const boost::system::error_code& ec, 122 const tcp::endpoint& endpoint) 123{ 124 // ... 125} 126</pre> 127</div> 128<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 129<td align="left"></td> 130<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 131 Kohlhoff<p> 132 Distributed under the Boost Software License, Version 1.0. (See accompanying 133 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 134 </p> 135</div></td> 136</tr></table> 137<hr> 138<div class="spirit-nav"> 139<a accesskey="p" href="../async_connect.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_connect.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 140</div> 141</body> 142</html> 143