• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>async_connect (6 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="overload5.html" title="async_connect (5 of 6 overloads)">
10<link rel="next" href="../async_initiate.html" title="async_initiate">
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="overload5.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="../async_initiate.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.overload6"></a><a class="link" href="overload6.html" title="async_connect (6 of 6 overloads)">async_connect
28        (6 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&lt;
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 Iterator,
38    typename <a class="link" href="../ConnectCondition.html" title="Connect condition requirements">ConnectCondition</a>,
39    typename <a class="link" href="../IteratorConnectHandler.html" title="Iterator connect handler requirements">IteratorConnectHandler</a> = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>&gt;
40<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(
41    basic_socket&lt; Protocol, Executor &gt; &amp; s,
42    Iterator begin,
43    Iterator end,
44    ConnectCondition connect_condition,
45    IteratorConnectHandler &amp;&amp; handler = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>);
46</pre>
47<p>
48          This function attempts to connect a socket to one of a sequence of endpoints.
49          It does this by repeated calls to the socket's <code class="computeroutput">async_connect</code>
50          member function, once for each endpoint in the sequence, until a connection
51          is successfully established.
52        </p>
53<h6>
54<a name="boost_asio.reference.async_connect.overload6.h0"></a>
55          <span class="phrase"><a name="boost_asio.reference.async_connect.overload6.parameters"></a></span><a class="link" href="overload6.html#boost_asio.reference.async_connect.overload6.parameters">Parameters</a>
56        </h6>
57<div class="variablelist">
58<p class="title"><b></b></p>
59<dl class="variablelist">
60<dt><span class="term">s</span></dt>
61<dd><p>
62                The socket to be connected. If the socket is already open, it will
63                be closed.
64              </p></dd>
65<dt><span class="term">begin</span></dt>
66<dd><p>
67                An iterator pointing to the start of a sequence of endpoints.
68              </p></dd>
69<dt><span class="term">end</span></dt>
70<dd><p>
71                An iterator pointing to the end of a sequence of endpoints.
72              </p></dd>
73<dt><span class="term">connect_condition</span></dt>
74<dd>
75<p>
76                A function object that is called prior to each connection attempt.
77                The signature of the function object must be:
78</p>
79<pre class="programlisting">bool connect_condition(
80    const boost::system::error_code&amp; ec,
81    const typename Protocol::endpoint&amp; next);
82</pre>
83<p>
84                The <code class="computeroutput">ec</code> parameter contains the result from the most recent
85                connect operation. Before the first connection attempt, <code class="computeroutput">ec</code>
86                is always set to indicate success. The <code class="computeroutput">next</code> parameter
87                is the next endpoint to be tried. The function object should return
88                true if the next endpoint should be tried, and false if it should
89                be skipped.
90              </p>
91</dd>
92<dt><span class="term">handler</span></dt>
93<dd>
94<p>
95                The handler to be called when the connect operation completes. Copies
96                will be made of the handler as required. The function signature of
97                the handler must be:
98</p>
99<pre class="programlisting">void handler(
100  // Result of operation. if the sequence is empty, set to
101  // boost::asio::error::not_found. Otherwise, contains the
102  // error from the last connection attempt.
103  const boost::system::error_code&amp; error,
104
105  // On success, an iterator denoting the successfully
106  // connected endpoint. Otherwise, the end iterator.
107  Iterator iterator
108);
109</pre>
110<p>
111                Regardless of whether the asynchronous operation completes immediately
112                or not, the handler will not be invoked from within this function.
113                On immediate completion, invocation of the handler will be performed
114                in a manner equivalent to using <a class="link" href="../post.html" title="post"><code class="computeroutput">post</code></a>.
115              </p>
116</dd>
117</dl>
118</div>
119<h6>
120<a name="boost_asio.reference.async_connect.overload6.h1"></a>
121          <span class="phrase"><a name="boost_asio.reference.async_connect.overload6.example"></a></span><a class="link" href="overload6.html#boost_asio.reference.async_connect.overload6.example">Example</a>
122        </h6>
123<p>
124          The following connect condition function object can be used to output information
125          about the individual connection attempts:
126        </p>
127<pre class="programlisting">struct my_connect_condition
128{
129  bool operator()(
130      const boost::system::error_code&amp; ec,
131      const::tcp::endpoint&amp; next)
132  {
133    if (ec) std::cout &lt;&lt; "Error: " &lt;&lt; ec.message() &lt;&lt; std::endl;
134    std::cout &lt;&lt; "Trying: " &lt;&lt; next &lt;&lt; std::endl;
135    return true;
136  }
137};
138</pre>
139<p>
140          It would be used with the <code class="computeroutput">boost::asio::connect</code> function as
141          follows:
142        </p>
143<pre class="programlisting">tcp::resolver r(my_context);
144tcp::resolver::query q("host", "service");
145tcp::socket s(my_context);
146
147// ...
148
149r.async_resolve(q, resolve_handler);
150
151// ...
152
153void resolve_handler(
154    const boost::system::error_code&amp; ec,
155    tcp::resolver::iterator i)
156{
157  if (!ec)
158  {
159    tcp::resolver::iterator end;
160    boost::asio::async_connect(s, i, end,
161        my_connect_condition(),
162        connect_handler);
163  }
164}
165
166// ...
167
168void connect_handler(
169    const boost::system::error_code&amp; ec,
170    tcp::resolver::iterator i)
171{
172  if (ec)
173  {
174    // An error occurred.
175  }
176  else
177  {
178    std::cout &lt;&lt; "Connected to: " &lt;&lt; i-&gt;endpoint() &lt;&lt; std::endl;
179  }
180}
181</pre>
182</div>
183<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
184<td align="left"></td>
185<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
186      Kohlhoff<p>
187        Distributed under the Boost Software License, Version 1.0. (See accompanying
188        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>)
189      </p>
190</div></td>
191</tr></table>
192<hr>
193<div class="spirit-nav">
194<a accesskey="p" href="overload5.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="../async_initiate.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
195</div>
196</body>
197</html>
198