1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Daytime.6 - An asynchronous UDP daytime server</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="../tutorial.html" title="Tutorial"> 9<link rel="prev" href="tutdaytime5/src.html" title="Source listing for Daytime.5"> 10<link rel="next" href="tutdaytime6/src.html" title="Source listing for Daytime.6"> 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="tutdaytime5/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tutdaytime6/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_asio.tutorial.tutdaytime6"></a><a class="link" href="tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">Daytime.6 - An asynchronous 28 UDP daytime server</a> 29</h3></div></div></div> 30<h5> 31<a name="boost_asio.tutorial.tutdaytime6.h0"></a> 32 <span class="phrase"><a name="boost_asio.tutorial.tutdaytime6.the_main___function"></a></span><a class="link" href="tutdaytime6.html#boost_asio.tutorial.tutdaytime6.the_main___function">The 33 main() function</a> 34 </h5> 35<pre class="programlisting">int main() 36{ 37 try 38 { 39</pre> 40<p> 41 Create a server object to accept incoming client requests, and run the <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> object. 42 </p> 43<pre class="programlisting"> boost::asio::io_context io_context; 44 udp_server server(io_context); 45 io_context.run(); 46 } 47 catch (std::exception& e) 48 { 49 std::cerr << e.what() << std::endl; 50 } 51 52 return 0; 53} 54</pre> 55<h5> 56<a name="boost_asio.tutorial.tutdaytime6.h1"></a> 57 <span class="phrase"><a name="boost_asio.tutorial.tutdaytime6.the_udp_server_class"></a></span><a class="link" href="tutdaytime6.html#boost_asio.tutorial.tutdaytime6.the_udp_server_class">The udp_server 58 class</a> 59 </h5> 60<pre class="programlisting">class udp_server 61{ 62public: 63</pre> 64<p> 65 The constructor initialises a socket to listen on UDP port 13. 66 </p> 67<pre class="programlisting"> udp_server(boost::asio::io_context& io_context) 68 : socket_(io_context, udp::endpoint(udp::v4(), 13)) 69 { 70 start_receive(); 71 } 72 73private: 74 void start_receive() 75 { 76</pre> 77<p> 78 The function <a class="link" href="../reference/basic_datagram_socket/async_receive_from.html" title="basic_datagram_socket::async_receive_from">ip::udp::socket::async_receive_from()</a> 79 will cause the application to listen in the background for a new request. 80 When such a request is received, the <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> 81 object will invoke the <code class="computeroutput">handle_receive()</code> function with two arguments: 82 a value of type boost::system::error_code indicating whether the operation 83 succeeded or failed, and a <code class="computeroutput">size_t</code> value <code class="computeroutput">bytes_transferred</code> 84 specifying the number of bytes received. 85 </p> 86<pre class="programlisting"> socket_.async_receive_from( 87 boost::asio::buffer(recv_buffer_), remote_endpoint_, 88 boost::bind(&udp_server::handle_receive, this, 89 boost::asio::placeholders::error, 90 boost::asio::placeholders::bytes_transferred)); 91 } 92</pre> 93<p> 94 The function <code class="computeroutput">handle_receive()</code> will service the client request. 95 </p> 96<pre class="programlisting"> void handle_receive(const boost::system::error_code& error, 97 std::size_t /*bytes_transferred*/) 98 { 99</pre> 100<p> 101 The <code class="computeroutput">error</code> parameter contains the result of the asynchronous 102 operation. Since we only provide the 1-byte <code class="computeroutput">recv_buffer_</code> to 103 contain the client's request, the <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> 104 object would return an error if the client sent anything larger. We can ignore 105 such an error if it comes up. 106 </p> 107<pre class="programlisting"> if (!error) 108 { 109</pre> 110<p> 111 Determine what we are going to send. 112 </p> 113<pre class="programlisting"> boost::shared_ptr<std::string> message( 114 new std::string(make_daytime_string())); 115</pre> 116<p> 117 We now call <a class="link" href="../reference/basic_datagram_socket/async_send_to.html" title="basic_datagram_socket::async_send_to">ip::udp::socket::async_send_to()</a> 118 to serve the data to the client. 119 </p> 120<pre class="programlisting"> socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_, 121 boost::bind(&udp_server::handle_send, this, message, 122 boost::asio::placeholders::error, 123 boost::asio::placeholders::bytes_transferred)); 124</pre> 125<p> 126 When initiating the asynchronous operation, and if using boost::bind(), you 127 must specify only the arguments that match the handler's parameter list. 128 In this program, both of the argument placeholders (boost::asio::placeholders::error 129 and boost::asio::placeholders::bytes_transferred) could potentially have 130 been removed. 131 </p> 132<p> 133 Start listening for the next client request. 134 </p> 135<pre class="programlisting"> start_receive(); 136</pre> 137<p> 138 Any further actions for this client request are now the responsibility of 139 <code class="computeroutput">handle_send()</code>. 140 </p> 141<pre class="programlisting"> } 142 } 143</pre> 144<p> 145 The function <code class="computeroutput">handle_send()</code> is invoked after the service request 146 has been completed. 147 </p> 148<pre class="programlisting"> void handle_send(boost::shared_ptr<std::string> /*message*/, 149 const boost::system::error_code& /*error*/, 150 std::size_t /*bytes_transferred*/) 151 { 152 } 153 154 udp::socket socket_; 155 udp::endpoint remote_endpoint_; 156 boost::array<char, 1> recv_buffer_; 157}; 158</pre> 159<p> 160 See the <a class="link" href="tutdaytime6/src.html" title="Source listing for Daytime.6">full source listing</a> 161 </p> 162<p> 163 Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> 164 </p> 165<p> 166 Previous: <a class="link" href="tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">Daytime.5 - A synchronous 167 UDP daytime server</a> 168 </p> 169<p> 170 Next: <a class="link" href="tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">Daytime.7 - A combined 171 TCP/UDP asynchronous server</a> 172 </p> 173</div> 174<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 175<td align="left"></td> 176<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 177 Kohlhoff<p> 178 Distributed under the Boost Software License, Version 1.0. (See accompanying 179 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>) 180 </p> 181</div></td> 182</tr></table> 183<hr> 184<div class="spirit-nav"> 185<a accesskey="p" href="tutdaytime5/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tutdaytime6/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 186</div> 187</body> 188</html> 189