• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>TCP, UDP and ICMP</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="../networking.html" title="Networking">
10<link rel="next" href="other_protocols.html" title="Support for Other Protocols">
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="../networking.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="other_protocols.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.protocols"></a><a class="link" href="protocols.html" title="TCP, UDP and ICMP">TCP, UDP
28        and ICMP</a>
29</h4></div></div></div>
30<p>
31          Boost.Asio provides off-the-shelf support for the internet protocols TCP,
32          UDP and ICMP.
33        </p>
34<h6>
35<a name="boost_asio.overview.networking.protocols.h0"></a>
36          <span class="phrase"><a name="boost_asio.overview.networking.protocols.tcp_clients"></a></span><a class="link" href="protocols.html#boost_asio.overview.networking.protocols.tcp_clients">TCP Clients</a>
37        </h6>
38<p>
39          Hostname resolution is performed using a resolver, where host and service
40          names are looked up and converted into one or more endpoints:
41        </p>
42<pre class="programlisting">ip::tcp::resolver resolver(my_io_context);
43ip::tcp::resolver::query query("www.boost.org", "http");
44ip::tcp::resolver::iterator iter = resolver.resolve(query);
45ip::tcp::resolver::iterator end; // End marker.
46while (iter != end)
47{
48  ip::tcp::endpoint endpoint = *iter++;
49  std::cout &lt;&lt; endpoint &lt;&lt; std::endl;
50}
51</pre>
52<p>
53          The list of endpoints obtained above could contain both IPv4 and IPv6 endpoints,
54          so a program should try each of them until it finds one that works. This
55          keeps the client program independent of a specific IP version.
56        </p>
57<p>
58          To simplify the development of protocol-independent programs, TCP clients
59          may establish connections using the free functions <a class="link" href="../../reference/connect.html" title="connect">connect()</a>
60          and <a class="link" href="../../reference/async_connect.html" title="async_connect">async_connect()</a>.
61          These operations try each endpoint in a list until the socket is successfully
62          connected. For example, a single call:
63        </p>
64<pre class="programlisting">ip::tcp::socket socket(my_io_context);
65boost::asio::connect(socket, resolver.resolve(query));
66</pre>
67<p>
68          will synchronously try all endpoints until one is successfully connected.
69          Similarly, an asynchronous connect may be performed by writing:
70        </p>
71<pre class="programlisting">boost::asio::async_connect(socket_, iter,
72    boost::bind(&amp;client::handle_connect, this,
73      boost::asio::placeholders::error));
74
75// ...
76
77void handle_connect(const error_code&amp; error)
78{
79  if (!error)
80  {
81    // Start read or write operations.
82  }
83  else
84  {
85    // Handle error.
86  }
87}
88</pre>
89<p>
90          When a specific endpoint is available, a socket can be created and connected:
91        </p>
92<pre class="programlisting">ip::tcp::socket socket(my_io_context);
93socket.connect(endpoint);
94</pre>
95<p>
96          Data may be read from or written to a connected TCP socket using the <a class="link" href="../../reference/basic_stream_socket/receive.html" title="basic_stream_socket::receive">receive()</a>,
97          <a class="link" href="../../reference/basic_stream_socket/async_receive.html" title="basic_stream_socket::async_receive">async_receive()</a>,
98          <a class="link" href="../../reference/basic_stream_socket/send.html" title="basic_stream_socket::send">send()</a>
99          or <a class="link" href="../../reference/basic_stream_socket/async_send.html" title="basic_stream_socket::async_send">async_send()</a>
100          member functions. However, as these could result in <a class="link" href="../core/streams.html" title="Streams, Short Reads and Short Writes">short
101          writes or reads</a>, an application will typically use the following
102          operations instead: <a class="link" href="../../reference/read.html" title="read">read()</a>,
103          <a class="link" href="../../reference/async_read.html" title="async_read">async_read()</a>, <a class="link" href="../../reference/write.html" title="write">write()</a> and <a class="link" href="../../reference/async_write.html" title="async_write">async_write()</a>.
104        </p>
105<h6>
106<a name="boost_asio.overview.networking.protocols.h1"></a>
107          <span class="phrase"><a name="boost_asio.overview.networking.protocols.tcp_servers"></a></span><a class="link" href="protocols.html#boost_asio.overview.networking.protocols.tcp_servers">TCP Servers</a>
108        </h6>
109<p>
110          A program uses an acceptor to accept incoming TCP connections:
111        </p>
112<pre class="programlisting">ip::tcp::acceptor acceptor(my_io_context, my_endpoint);
113...
114ip::tcp::socket socket(my_io_context);
115acceptor.accept(socket);
116</pre>
117<p>
118          After a socket has been successfully accepted, it may be read from or written
119          to as illustrated for TCP clients above.
120        </p>
121<h6>
122<a name="boost_asio.overview.networking.protocols.h2"></a>
123          <span class="phrase"><a name="boost_asio.overview.networking.protocols.udp"></a></span><a class="link" href="protocols.html#boost_asio.overview.networking.protocols.udp">UDP</a>
124        </h6>
125<p>
126          UDP hostname resolution is also performed using a resolver:
127        </p>
128<pre class="programlisting">ip::udp::resolver resolver(my_io_context);
129ip::udp::resolver::query query("localhost", "daytime");
130ip::udp::resolver::iterator iter = resolver.resolve(query);
131...
132</pre>
133<p>
134          A UDP socket is typically bound to a local endpoint. The following code
135          will create an IP version 4 UDP socket and bind it to the "any"
136          address on port <code class="computeroutput">12345</code>:
137        </p>
138<pre class="programlisting">ip::udp::endpoint endpoint(ip::udp::v4(), 12345);
139ip::udp::socket socket(my_io_context, endpoint);
140</pre>
141<p>
142          Data may be read from or written to an unconnected UDP socket using the
143          <a class="link" href="../../reference/basic_datagram_socket/receive_from.html" title="basic_datagram_socket::receive_from">receive_from()</a>,
144          <a class="link" href="../../reference/basic_datagram_socket/async_receive_from.html" title="basic_datagram_socket::async_receive_from">async_receive_from()</a>,
145          <a class="link" href="../../reference/basic_datagram_socket/send_to.html" title="basic_datagram_socket::send_to">send_to()</a>
146          or <a class="link" href="../../reference/basic_datagram_socket/async_send_to.html" title="basic_datagram_socket::async_send_to">async_send_to()</a>
147          member functions. For a connected UDP socket, use the <a class="link" href="../../reference/basic_datagram_socket/receive.html" title="basic_datagram_socket::receive">receive()</a>,
148          <a class="link" href="../../reference/basic_datagram_socket/async_receive.html" title="basic_datagram_socket::async_receive">async_receive()</a>,
149          <a class="link" href="../../reference/basic_datagram_socket/send.html" title="basic_datagram_socket::send">send()</a>
150          or <a class="link" href="../../reference/basic_datagram_socket/async_send.html" title="basic_datagram_socket::async_send">async_send()</a>
151          member functions.
152        </p>
153<h6>
154<a name="boost_asio.overview.networking.protocols.h3"></a>
155          <span class="phrase"><a name="boost_asio.overview.networking.protocols.icmp"></a></span><a class="link" href="protocols.html#boost_asio.overview.networking.protocols.icmp">ICMP</a>
156        </h6>
157<p>
158          As with TCP and UDP, ICMP hostname resolution is performed using a resolver:
159        </p>
160<pre class="programlisting">ip::icmp::resolver resolver(my_io_context);
161ip::icmp::resolver::query query("localhost", "");
162ip::icmp::resolver::iterator iter = resolver.resolve(query);
163...
164</pre>
165<p>
166          An ICMP socket may be bound to a local endpoint. The following code will
167          create an IP version 6 ICMP socket and bind it to the "any" address:
168        </p>
169<pre class="programlisting">ip::icmp::endpoint endpoint(ip::icmp::v6(), 0);
170ip::icmp::socket socket(my_io_context, endpoint);
171</pre>
172<p>
173          The port number is not used for ICMP.
174        </p>
175<p>
176          Data may be read from or written to an unconnected ICMP socket using the
177          <a class="link" href="../../reference/basic_raw_socket/receive_from.html" title="basic_raw_socket::receive_from">receive_from()</a>,
178          <a class="link" href="../../reference/basic_raw_socket/async_receive_from.html" title="basic_raw_socket::async_receive_from">async_receive_from()</a>,
179          <a class="link" href="../../reference/basic_raw_socket/send_to.html" title="basic_raw_socket::send_to">send_to()</a>
180          or <a class="link" href="../../reference/basic_raw_socket/async_send_to.html" title="basic_raw_socket::async_send_to">async_send_to()</a>
181          member functions.
182        </p>
183<h6>
184<a name="boost_asio.overview.networking.protocols.h4"></a>
185          <span class="phrase"><a name="boost_asio.overview.networking.protocols.see_also"></a></span><a class="link" href="protocols.html#boost_asio.overview.networking.protocols.see_also">See
186          Also</a>
187        </h6>
188<p>
189          <a class="link" href="../../reference/ip__tcp.html" title="ip::tcp">ip::tcp</a>, <a class="link" href="../../reference/ip__udp.html" title="ip::udp">ip::udp</a>,
190          <a class="link" href="../../reference/ip__icmp.html" title="ip::icmp">ip::icmp</a>, <a class="link" href="../../tutorial/tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client">daytime
191          protocol tutorials</a>, <a class="link" href="../../examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.icmp">ICMP
192          ping example</a>.
193        </p>
194</div>
195<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
196<td align="left"></td>
197<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
198      Kohlhoff<p>
199        Distributed under the Boost Software License, Version 1.0. (See accompanying
200        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>)
201      </p>
202</div></td>
203</tr></table>
204<hr>
205<div class="spirit-nav">
206<a accesskey="p" href="../networking.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="other_protocols.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
207</div>
208</body>
209</html>
210