1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Support for Other Protocols</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="../networking.html" title="Networking"> 9<link rel="prev" href="protocols.html" title="TCP, UDP and ICMP"> 10<link rel="next" href="iostreams.html" title="Socket Iostreams"> 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="protocols.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../networking.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="iostreams.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.overview.networking.other_protocols"></a><a class="link" href="other_protocols.html" title="Support for Other Protocols">Support 28 for Other Protocols</a> 29</h4></div></div></div> 30<p> 31 Support for other socket protocols (such as Bluetooth or IRCOMM sockets) 32 can be added by implementing the <a class="link" href="../../reference/Protocol.html" title="Protocol requirements">protocol 33 type requirements</a>. However, in many cases these protocols may also 34 be used with Boost.Asio's generic protocol support. For this, Boost.Asio 35 provides the following four classes: 36 </p> 37<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 38<li class="listitem"> 39 <a class="link" href="../../reference/generic__datagram_protocol.html" title="generic::datagram_protocol"><code class="computeroutput">generic::datagram_protocol</code></a> 40 </li> 41<li class="listitem"> 42 <a class="link" href="../../reference/generic__raw_protocol.html" title="generic::raw_protocol"><code class="computeroutput">generic::raw_protocol</code></a> 43 </li> 44<li class="listitem"> 45 <a class="link" href="../../reference/generic__seq_packet_protocol.html" title="generic::seq_packet_protocol"><code class="computeroutput">generic::seq_packet_protocol</code></a> 46 </li> 47<li class="listitem"> 48 <a class="link" href="../../reference/generic__stream_protocol.html" title="generic::stream_protocol"><code class="computeroutput">generic::stream_protocol</code></a> 49 </li> 50</ul></div> 51<p> 52 These classes implement the <a class="link" href="../../reference/Protocol.html" title="Protocol requirements">protocol 53 type requirements</a>, but allow the user to specify the address family 54 (e.g. <code class="computeroutput">AF_INET</code>) and protocol type (e.g. <code class="computeroutput">IPPROTO_TCP</code>) 55 at runtime. For example: 56 </p> 57<pre class="programlisting">boost::asio::generic::stream_protocol::socket my_socket(my_io_context); 58my_socket.open(boost::asio::generic::stream_protocol(AF_INET, IPPROTO_TCP)); 59... 60</pre> 61<p> 62 An endpoint class template, <a class="link" href="../../reference/generic__basic_endpoint.html" title="generic::basic_endpoint"><code class="computeroutput">boost::asio::generic::basic_endpoint</code></a>, 63 is included to support these protocol classes. This endpoint can hold any 64 other endpoint type, provided its native representation fits into a <code class="computeroutput">sockaddr_storage</code> 65 object. This class will also convert from other types that implement the 66 <a class="link" href="../../reference/Endpoint.html" title="Endpoint requirements">endpoint</a> type requirements: 67 </p> 68<pre class="programlisting">boost::asio::ip::tcp::endpoint my_endpoint1 = ...; 69boost::asio::generic::stream_protocol::endpoint my_endpoint2(my_endpoint1); 70</pre> 71<p> 72 The conversion is implicit, so as to support the following use cases: 73 </p> 74<pre class="programlisting">boost::asio::generic::stream_protocol::socket my_socket(my_io_context); 75boost::asio::ip::tcp::endpoint my_endpoint = ...; 76my_socket.connect(my_endpoint); 77</pre> 78<h6> 79<a name="boost_asio.overview.networking.other_protocols.h0"></a> 80 <span class="phrase"><a name="boost_asio.overview.networking.other_protocols.c__11_move_construction"></a></span><a class="link" href="other_protocols.html#boost_asio.overview.networking.other_protocols.c__11_move_construction">C++11 81 Move Construction</a> 82 </h6> 83<p> 84 When using C++11, it is possible to perform move construction from a socket 85 (or acceptor) object to convert to the more generic protocol's socket (or 86 acceptor) type. If the protocol conversion is valid: 87 </p> 88<pre class="programlisting">Protocol1 p1 = ...; 89Protocol2 p2(p1); 90</pre> 91<p> 92 then the corresponding socket conversion is allowed: 93 </p> 94<pre class="programlisting">Protocol1::socket my_socket1(my_io_context); 95... 96Protocol2::socket my_socket2(std::move(my_socket1)); 97</pre> 98<p> 99 For example, one possible conversion is from a TCP socket to a generic 100 stream-oriented socket: 101 </p> 102<pre class="programlisting">boost::asio::ip::tcp::socket my_socket1(my_io_context); 103... 104boost::asio::generic::stream_protocol::socket my_socket2(std::move(my_socket1)); 105</pre> 106<p> 107 These conversions are also available for move-assignment. 108 </p> 109<p> 110 These conversions are not limited to the above generic protocol classes. 111 User-defined protocols may take advantage of this feature by similarly 112 ensuring the conversion from <code class="computeroutput">Protocol1</code> to <code class="computeroutput">Protocol2</code> 113 is valid, as above. 114 </p> 115<h6> 116<a name="boost_asio.overview.networking.other_protocols.h1"></a> 117 <span class="phrase"><a name="boost_asio.overview.networking.other_protocols.accepting_generic_sockets"></a></span><a class="link" href="other_protocols.html#boost_asio.overview.networking.other_protocols.accepting_generic_sockets">Accepting 118 Generic Sockets</a> 119 </h6> 120<p> 121 As a convenience, a socket acceptor's <code class="computeroutput">accept()</code> and <code class="computeroutput">async_accept()</code> 122 functions can directly accept into a different protocol's socket type, 123 provided the corresponding protocol conversion is valid. For example, the 124 following is supported because the protocol <code class="computeroutput">boost::asio::ip::tcp</code> 125 is convertible to <code class="computeroutput">boost::asio::generic::stream_protocol</code>: 126 </p> 127<pre class="programlisting">boost::asio::ip::tcp::acceptor my_acceptor(my_io_context); 128... 129boost::asio::generic::stream_protocol::socket my_socket(my_io_context); 130my_acceptor.accept(my_socket); 131</pre> 132<h6> 133<a name="boost_asio.overview.networking.other_protocols.h2"></a> 134 <span class="phrase"><a name="boost_asio.overview.networking.other_protocols.see_also"></a></span><a class="link" href="other_protocols.html#boost_asio.overview.networking.other_protocols.see_also">See Also</a> 135 </h6> 136<p> 137 <a class="link" href="../../reference/generic__datagram_protocol.html" title="generic::datagram_protocol"><code class="computeroutput">generic::datagram_protocol</code></a>, 138 <a class="link" href="../../reference/generic__raw_protocol.html" title="generic::raw_protocol"><code class="computeroutput">generic::raw_protocol</code></a>, 139 <a class="link" href="../../reference/generic__seq_packet_protocol.html" title="generic::seq_packet_protocol"><code class="computeroutput">generic::seq_packet_protocol</code></a>, 140 <a class="link" href="../../reference/generic__stream_protocol.html" title="generic::stream_protocol"><code class="computeroutput">generic::stream_protocol</code></a>, 141 <a class="link" href="../../reference/Protocol.html" title="Protocol requirements">protocol type requirements</a>. 142 </p> 143</div> 144<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 145<td align="left"></td> 146<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 147 Kohlhoff<p> 148 Distributed under the Boost Software License, Version 1.0. (See accompanying 149 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>) 150 </p> 151</div></td> 152</tr></table> 153<hr> 154<div class="spirit-nav"> 155<a accesskey="p" href="protocols.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../networking.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="iostreams.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 156</div> 157</body> 158</html> 159