• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Source listing for Daytime.7</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="../tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">
9<link rel="prev" href="../tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">
10<link rel="next" href="../../examples.html" title="Examples">
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="../tutdaytime7.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime7.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="../../examples.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.tutorial.tutdaytime7.src"></a><a class="link" href="src.html" title="Source listing for Daytime.7">Source listing
28        for Daytime.7</a>
29</h4></div></div></div>
30<pre class="programlisting">//
31// server.cpp
32// ~~~~~~~~~~
33//
34// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
35//
36// Distributed under the Boost Software License, Version 1.0. (See accompanying
37// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
38//
39
40#include &lt;ctime&gt;
41#include &lt;iostream&gt;
42#include &lt;string&gt;
43#include &lt;boost/array.hpp&gt;
44#include &lt;boost/bind/bind.hpp&gt;
45#include &lt;boost/shared_ptr.hpp&gt;
46#include &lt;boost/enable_shared_from_this.hpp&gt;
47#include &lt;boost/asio.hpp&gt;
48
49using boost::asio::ip::tcp;
50using boost::asio::ip::udp;
51
52std::string make_daytime_string()
53{
54  using namespace std; // For time_t, time and ctime;
55  time_t now = time(0);
56  return ctime(&amp;now);
57}
58
59class tcp_connection
60  : public boost::enable_shared_from_this&lt;tcp_connection&gt;
61{
62public:
63  typedef boost::shared_ptr&lt;tcp_connection&gt; pointer;
64
65  static pointer create(boost::asio::io_context&amp; io_context)
66  {
67    return pointer(new tcp_connection(io_context));
68  }
69
70  tcp::socket&amp; socket()
71  {
72    return socket_;
73  }
74
75  void start()
76  {
77    message_ = make_daytime_string();
78
79    boost::asio::async_write(socket_, boost::asio::buffer(message_),
80        boost::bind(&amp;tcp_connection::handle_write, shared_from_this()));
81  }
82
83private:
84  tcp_connection(boost::asio::io_context&amp; io_context)
85    : socket_(io_context)
86  {
87  }
88
89  void handle_write()
90  {
91  }
92
93  tcp::socket socket_;
94  std::string message_;
95};
96
97class tcp_server
98{
99public:
100  tcp_server(boost::asio::io_context&amp; io_context)
101    : io_context_(io_context),
102      acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
103  {
104    start_accept();
105  }
106
107private:
108  void start_accept()
109  {
110    tcp_connection::pointer new_connection =
111      tcp_connection::create(io_context_);
112
113    acceptor_.async_accept(new_connection-&gt;socket(),
114        boost::bind(&amp;tcp_server::handle_accept, this, new_connection,
115          boost::asio::placeholders::error));
116  }
117
118  void handle_accept(tcp_connection::pointer new_connection,
119      const boost::system::error_code&amp; error)
120  {
121    if (!error)
122    {
123      new_connection-&gt;start();
124    }
125
126    start_accept();
127  }
128
129  boost::asio::io_context&amp; io_context_;
130  tcp::acceptor acceptor_;
131};
132
133class udp_server
134{
135public:
136  udp_server(boost::asio::io_context&amp; io_context)
137    : socket_(io_context, udp::endpoint(udp::v4(), 13))
138  {
139    start_receive();
140  }
141
142private:
143  void start_receive()
144  {
145    socket_.async_receive_from(
146        boost::asio::buffer(recv_buffer_), remote_endpoint_,
147        boost::bind(&amp;udp_server::handle_receive, this,
148          boost::asio::placeholders::error));
149  }
150
151  void handle_receive(const boost::system::error_code&amp; error)
152  {
153    if (!error)
154    {
155      boost::shared_ptr&lt;std::string&gt; message(
156          new std::string(make_daytime_string()));
157
158      socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
159          boost::bind(&amp;udp_server::handle_send, this, message));
160
161      start_receive();
162    }
163  }
164
165  void handle_send(boost::shared_ptr&lt;std::string&gt; /*message*/)
166  {
167  }
168
169  udp::socket socket_;
170  udp::endpoint remote_endpoint_;
171  boost::array&lt;char, 1&gt; recv_buffer_;
172};
173
174int main()
175{
176  try
177  {
178    boost::asio::io_context io_context;
179    tcp_server server1(io_context);
180    udp_server server2(io_context);
181    io_context.run();
182  }
183  catch (std::exception&amp; e)
184  {
185    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
186  }
187
188  return 0;
189}
190</pre>
191<p>
192          Return to <a class="link" href="../tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">Daytime.7 - A
193          combined TCP/UDP asynchronous server</a>
194        </p>
195</div>
196<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
197<td align="left"></td>
198<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
199      Kohlhoff<p>
200        Distributed under the Boost Software License, Version 1.0. (See accompanying
201        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>)
202      </p>
203</div></td>
204</tr></table>
205<hr>
206<div class="spirit-nav">
207<a accesskey="p" href="../tutdaytime7.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime7.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="../../examples.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
208</div>
209</body>
210</html>
211