• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>connect (7 of 12 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="../connect.html" title="connect">
9<link rel="prev" href="overload6.html" title="connect (6 of 12 overloads)">
10<link rel="next" href="overload8.html" title="connect (8 of 12 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="overload6.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../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="overload8.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.connect.overload7"></a><a class="link" href="overload7.html" title="connect (7 of 12 overloads)">connect (7
28        of 12 overloads)</a>
29</h4></div></div></div>
30<p>
31          Establishes a socket connection by trying each endpoint in a sequence.
32        </p>
33<pre class="programlisting">template&lt;
34    typename <a class="link" href="../Protocol.html" title="Protocol requirements">Protocol</a>,
35    typename <a class="link" href="../Executor1.html" title="Executor requirements">Executor</a>,
36    typename <a class="link" href="../EndpointSequence.html" title="Endpoint sequence requirements">EndpointSequence</a>,
37    typename <a class="link" href="../ConnectCondition.html" title="Connect condition requirements">ConnectCondition</a>&gt;
38Protocol::endpoint connect(
39    basic_socket&lt; Protocol, Executor &gt; &amp; s,
40    const EndpointSequence &amp; endpoints,
41    ConnectCondition connect_condition,
42    typename enable_if&lt; is_endpoint_sequence&lt; EndpointSequence &gt;::value &gt;::type *  = 0);
43</pre>
44<p>
45          This function attempts to connect a socket to one of a sequence of endpoints.
46          It does this by repeated calls to the socket's <code class="computeroutput">connect</code> member
47          function, once for each endpoint in the sequence, until a connection is
48          successfully established.
49        </p>
50<h6>
51<a name="boost_asio.reference.connect.overload7.h0"></a>
52          <span class="phrase"><a name="boost_asio.reference.connect.overload7.parameters"></a></span><a class="link" href="overload7.html#boost_asio.reference.connect.overload7.parameters">Parameters</a>
53        </h6>
54<div class="variablelist">
55<p class="title"><b></b></p>
56<dl class="variablelist">
57<dt><span class="term">s</span></dt>
58<dd><p>
59                The socket to be connected. If the socket is already open, it will
60                be closed.
61              </p></dd>
62<dt><span class="term">endpoints</span></dt>
63<dd><p>
64                A sequence of endpoints.
65              </p></dd>
66<dt><span class="term">connect_condition</span></dt>
67<dd>
68<p>
69                A function object that is called prior to each connection attempt.
70                The signature of the function object must be:
71</p>
72<pre class="programlisting">bool connect_condition(
73    const boost::system::error_code&amp; ec,
74    const typename Protocol::endpoint&amp; next);
75</pre>
76<p>
77                The <code class="computeroutput">ec</code> parameter contains the result from the most recent
78                connect operation. Before the first connection attempt, <code class="computeroutput">ec</code>
79                is always set to indicate success. The <code class="computeroutput">next</code> parameter
80                is the next endpoint to be tried. The function object should return
81                true if the next endpoint should be tried, and false if it should
82                be skipped.
83              </p>
84</dd>
85</dl>
86</div>
87<h6>
88<a name="boost_asio.reference.connect.overload7.h1"></a>
89          <span class="phrase"><a name="boost_asio.reference.connect.overload7.return_value"></a></span><a class="link" href="overload7.html#boost_asio.reference.connect.overload7.return_value">Return Value</a>
90        </h6>
91<p>
92          The successfully connected endpoint.
93        </p>
94<h6>
95<a name="boost_asio.reference.connect.overload7.h2"></a>
96          <span class="phrase"><a name="boost_asio.reference.connect.overload7.exceptions"></a></span><a class="link" href="overload7.html#boost_asio.reference.connect.overload7.exceptions">Exceptions</a>
97        </h6>
98<div class="variablelist">
99<p class="title"><b></b></p>
100<dl class="variablelist">
101<dt><span class="term">boost::system::system_error</span></dt>
102<dd><p>
103                Thrown on failure. If the sequence is empty, the associated <code class="computeroutput">error_code</code>
104                is <code class="computeroutput">boost::asio::error::not_found</code>. Otherwise, contains
105                the error from the last connection attempt.
106              </p></dd>
107</dl>
108</div>
109<h6>
110<a name="boost_asio.reference.connect.overload7.h3"></a>
111          <span class="phrase"><a name="boost_asio.reference.connect.overload7.example"></a></span><a class="link" href="overload7.html#boost_asio.reference.connect.overload7.example">Example</a>
112        </h6>
113<p>
114          The following connect condition function object can be used to output information
115          about the individual connection attempts:
116        </p>
117<pre class="programlisting">struct my_connect_condition
118{
119  bool operator()(
120      const boost::system::error_code&amp; ec,
121      const::tcp::endpoint&amp; next)
122  {
123    if (ec) std::cout &lt;&lt; "Error: " &lt;&lt; ec.message() &lt;&lt; std::endl;
124    std::cout &lt;&lt; "Trying: " &lt;&lt; next &lt;&lt; std::endl;
125    return true;
126  }
127};
128</pre>
129<p>
130          It would be used with the <code class="computeroutput">boost::asio::connect</code> function as
131          follows:
132        </p>
133<pre class="programlisting">tcp::resolver r(my_context);
134tcp::resolver::query q("host", "service");
135tcp::socket s(my_context);
136tcp::endpoint e = boost::asio::connect(s,
137    r.resolve(q), my_connect_condition());
138std::cout &lt;&lt; "Connected to: " &lt;&lt; e &lt;&lt; std::endl;
139</pre>
140</div>
141<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
142<td align="left"></td>
143<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
144      Kohlhoff<p>
145        Distributed under the Boost Software License, Version 1.0. (See accompanying
146        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>)
147      </p>
148</div></td>
149</tr></table>
150<hr>
151<div class="spirit-nav">
152<a accesskey="p" href="overload6.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../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="overload8.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
153</div>
154</body>
155</html>
156