1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>connect (8 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="overload7.html" title="connect (7 of 12 overloads)"> 10<link rel="next" href="overload9.html" title="connect (9 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="overload7.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="overload9.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.overload8"></a><a class="link" href="overload8.html" title="connect (8 of 12 overloads)">connect (8 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< 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>> 38Protocol::endpoint connect( 39 basic_socket< Protocol, Executor > & s, 40 const EndpointSequence & endpoints, 41 ConnectCondition connect_condition, 42 boost::system::error_code & ec, 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">connect</code> member 48 function, once for each endpoint in the sequence, until a connection is 49 successfully established. 50 </p> 51<h6> 52<a name="boost_asio.reference.connect.overload8.h0"></a> 53 <span class="phrase"><a name="boost_asio.reference.connect.overload8.parameters"></a></span><a class="link" href="overload8.html#boost_asio.reference.connect.overload8.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">connect_condition</span></dt> 68<dd> 69<p> 70 A function object that is called prior to each connection attempt. 71 The signature of the function object must be: 72</p> 73<pre class="programlisting">bool connect_condition( 74 const boost::system::error_code& ec, 75 const typename Protocol::endpoint& next); 76</pre> 77<p> 78 The <code class="computeroutput">ec</code> parameter contains the result from the most recent 79 connect operation. Before the first connection attempt, <code class="computeroutput">ec</code> 80 is always set to indicate success. The <code class="computeroutput">next</code> parameter 81 is the next endpoint to be tried. The function object should return 82 true if the next endpoint should be tried, and false if it should 83 be skipped. 84 </p> 85</dd> 86<dt><span class="term">ec</span></dt> 87<dd><p> 88 Set to indicate what error occurred, if any. If the sequence is empty, 89 set to <code class="computeroutput">boost::asio::error::not_found</code>. Otherwise, contains 90 the error from the last connection attempt. 91 </p></dd> 92</dl> 93</div> 94<h6> 95<a name="boost_asio.reference.connect.overload8.h1"></a> 96 <span class="phrase"><a name="boost_asio.reference.connect.overload8.return_value"></a></span><a class="link" href="overload8.html#boost_asio.reference.connect.overload8.return_value">Return Value</a> 97 </h6> 98<p> 99 On success, the successfully connected endpoint. Otherwise, a default-constructed 100 endpoint. 101 </p> 102<h6> 103<a name="boost_asio.reference.connect.overload8.h2"></a> 104 <span class="phrase"><a name="boost_asio.reference.connect.overload8.example"></a></span><a class="link" href="overload8.html#boost_asio.reference.connect.overload8.example">Example</a> 105 </h6> 106<p> 107 The following connect condition function object can be used to output information 108 about the individual connection attempts: 109 </p> 110<pre class="programlisting">struct my_connect_condition 111{ 112 bool operator()( 113 const boost::system::error_code& ec, 114 const::tcp::endpoint& next) 115 { 116 if (ec) std::cout << "Error: " << ec.message() << std::endl; 117 std::cout << "Trying: " << next << std::endl; 118 return true; 119 } 120}; 121</pre> 122<p> 123 It would be used with the <code class="computeroutput">boost::asio::connect</code> function as 124 follows: 125 </p> 126<pre class="programlisting">tcp::resolver r(my_context); 127tcp::resolver::query q("host", "service"); 128tcp::socket s(my_context); 129boost::system::error_code ec; 130tcp::endpoint e = boost::asio::connect(s, 131 r.resolve(q), my_connect_condition(), ec); 132if (ec) 133{ 134 // An error occurred. 135} 136else 137{ 138 std::cout << "Connected to: " << e << std::endl; 139} 140</pre> 141</div> 142<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 143<td align="left"></td> 144<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 145 Kohlhoff<p> 146 Distributed under the Boost Software License, Version 1.0. (See accompanying 147 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>) 148 </p> 149</div></td> 150</tr></table> 151<hr> 152<div class="spirit-nav"> 153<a accesskey="p" href="overload7.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="overload9.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 154</div> 155</body> 156</html> 157