1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Daytime.7 - A combined TCP/UDP asynchronous 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="tutdaytime6/src.html" title="Source listing for Daytime.6"> 10<link rel="next" href="tutdaytime7/src.html" title="Source listing for Daytime.7"> 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="tutdaytime6/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="tutdaytime7/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.tutdaytime7"></a><a class="link" href="tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">Daytime.7 - A combined 28 TCP/UDP asynchronous server</a> 29</h3></div></div></div> 30<p> 31 This tutorial program shows how to combine the two asynchronous servers that 32 we have just written, into a single server application. 33 </p> 34<h5> 35<a name="boost_asio.tutorial.tutdaytime7.h0"></a> 36 <span class="phrase"><a name="boost_asio.tutorial.tutdaytime7.the_main___function"></a></span><a class="link" href="tutdaytime7.html#boost_asio.tutorial.tutdaytime7.the_main___function">The 37 main() function</a> 38 </h5> 39<pre class="programlisting">int main() 40{ 41 try 42 { 43 boost::asio::io_context io_context; 44</pre> 45<p> 46 We will begin by creating a server object to accept a TCP client connection. 47 </p> 48<pre class="programlisting"> tcp_server server1(io_context); 49</pre> 50<p> 51 We also need a server object to accept a UDP client request. 52 </p> 53<pre class="programlisting"> udp_server server2(io_context); 54</pre> 55<p> 56 We have created two lots of work for the <a class="link" href="../reference/io_context.html" title="io_context">io_context</a> 57 object to do. 58 </p> 59<pre class="programlisting"> io_context.run(); 60 } 61 catch (std::exception& e) 62 { 63 std::cerr << e.what() << std::endl; 64 } 65 66 return 0; 67} 68</pre> 69<h5> 70<a name="boost_asio.tutorial.tutdaytime7.h1"></a> 71 <span class="phrase"><a name="boost_asio.tutorial.tutdaytime7.the_tcp_connection_and_tcp_server_classes"></a></span><a class="link" href="tutdaytime7.html#boost_asio.tutorial.tutdaytime7.the_tcp_connection_and_tcp_server_classes">The 72 tcp_connection and tcp_server classes</a> 73 </h5> 74<p> 75 The following two classes are taken from <a class="link" href="tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server">Daytime.3</a> 76 . 77 </p> 78<pre class="programlisting">class tcp_connection 79 : public boost::enable_shared_from_this<tcp_connection> 80{ 81public: 82 typedef boost::shared_ptr<tcp_connection> pointer; 83 84 static pointer create(boost::asio::io_context& io_context) 85 { 86 return pointer(new tcp_connection(io_context)); 87 } 88 89 tcp::socket& socket() 90 { 91 return socket_; 92 } 93 94 void start() 95 { 96 message_ = make_daytime_string(); 97 98 boost::asio::async_write(socket_, boost::asio::buffer(message_), 99 boost::bind(&tcp_connection::handle_write, shared_from_this())); 100 } 101 102private: 103 tcp_connection(boost::asio::io_context& io_context) 104 : socket_(io_context) 105 { 106 } 107 108 void handle_write() 109 { 110 } 111 112 tcp::socket socket_; 113 std::string message_; 114}; 115 116class tcp_server 117{ 118public: 119 tcp_server(boost::asio::io_context& io_context) 120 : io_context_(io_context), 121 acceptor_(io_context, tcp::endpoint(tcp::v4(), 13)) 122 { 123 start_accept(); 124 } 125 126private: 127 void start_accept() 128 { 129 tcp_connection::pointer new_connection = 130 tcp_connection::create(io_context_); 131 132 acceptor_.async_accept(new_connection->socket(), 133 boost::bind(&tcp_server::handle_accept, this, new_connection, 134 boost::asio::placeholders::error)); 135 } 136 137 void handle_accept(tcp_connection::pointer new_connection, 138 const boost::system::error_code& error) 139 { 140 if (!error) 141 { 142 new_connection->start(); 143 } 144 145 start_accept(); 146 } 147 148 boost::asio::io_context& io_context_; 149 tcp::acceptor acceptor_; 150}; 151</pre> 152<h5> 153<a name="boost_asio.tutorial.tutdaytime7.h2"></a> 154 <span class="phrase"><a name="boost_asio.tutorial.tutdaytime7.the_udp_server_class"></a></span><a class="link" href="tutdaytime7.html#boost_asio.tutorial.tutdaytime7.the_udp_server_class">The udp_server 155 class</a> 156 </h5> 157<p> 158 Similarly, this next class is taken from the <a class="link" href="tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">previous 159 tutorial step</a> . 160 </p> 161<pre class="programlisting">class udp_server 162{ 163public: 164 udp_server(boost::asio::io_context& io_context) 165 : socket_(io_context, udp::endpoint(udp::v4(), 13)) 166 { 167 start_receive(); 168 } 169 170private: 171 void start_receive() 172 { 173 socket_.async_receive_from( 174 boost::asio::buffer(recv_buffer_), remote_endpoint_, 175 boost::bind(&udp_server::handle_receive, this, 176 boost::asio::placeholders::error)); 177 } 178 179 void handle_receive(const boost::system::error_code& error) 180 { 181 if (!error) 182 { 183 boost::shared_ptr<std::string> message( 184 new std::string(make_daytime_string())); 185 186 socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_, 187 boost::bind(&udp_server::handle_send, this, message)); 188 189 start_receive(); 190 } 191 } 192 193 void handle_send(boost::shared_ptr<std::string> /*message*/) 194 { 195 } 196 197 udp::socket socket_; 198 udp::endpoint remote_endpoint_; 199 boost::array<char, 1> recv_buffer_; 200}; 201</pre> 202<p> 203 See the <a class="link" href="tutdaytime7/src.html" title="Source listing for Daytime.7">full source listing</a> 204 </p> 205<p> 206 Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> 207 </p> 208<p> 209 Previous: <a class="link" href="tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">Daytime.6 - An 210 asynchronous UDP daytime server</a> 211 </p> 212</div> 213<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 214<td align="left"></td> 215<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 216 Kohlhoff<p> 217 Distributed under the Boost Software License, Version 1.0. (See accompanying 218 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>) 219 </p> 220</div></td> 221</tr></table> 222<hr> 223<div class="spirit-nav"> 224<a accesskey="p" href="tutdaytime6/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="tutdaytime7/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 225</div> 226</body> 227</html> 228