• 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.6</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="../tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">
9<link rel="prev" href="../tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">
10<link rel="next" href="../tutdaytime7.html" title="Daytime.7 - A combined TCP/UDP asynchronous server">
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.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime6.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.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.tutdaytime6.src"></a><a class="link" href="src.html" title="Source listing for Daytime.6">Source listing
28        for Daytime.6</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/asio.hpp&gt;
47
48using boost::asio::ip::udp;
49
50std::string make_daytime_string()
51{
52  using namespace std; // For time_t, time and ctime;
53  time_t now = time(0);
54  return ctime(&amp;now);
55}
56
57class udp_server
58{
59public:
60  udp_server(boost::asio::io_context&amp; io_context)
61    : socket_(io_context, udp::endpoint(udp::v4(), 13))
62  {
63    start_receive();
64  }
65
66private:
67  void start_receive()
68  {
69    socket_.async_receive_from(
70        boost::asio::buffer(recv_buffer_), remote_endpoint_,
71        boost::bind(&amp;udp_server::handle_receive, this,
72          boost::asio::placeholders::error,
73          boost::asio::placeholders::bytes_transferred));
74  }
75
76  void handle_receive(const boost::system::error_code&amp; error,
77      std::size_t /*bytes_transferred*/)
78  {
79    if (!error)
80    {
81      boost::shared_ptr&lt;std::string&gt; message(
82          new std::string(make_daytime_string()));
83
84      socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
85          boost::bind(&amp;udp_server::handle_send, this, message,
86            boost::asio::placeholders::error,
87            boost::asio::placeholders::bytes_transferred));
88
89      start_receive();
90    }
91  }
92
93  void handle_send(boost::shared_ptr&lt;std::string&gt; /*message*/,
94      const boost::system::error_code&amp; /*error*/,
95      std::size_t /*bytes_transferred*/)
96  {
97  }
98
99  udp::socket socket_;
100  udp::endpoint remote_endpoint_;
101  boost::array&lt;char, 1&gt; recv_buffer_;
102};
103
104int main()
105{
106  try
107  {
108    boost::asio::io_context io_context;
109    udp_server server(io_context);
110    io_context.run();
111  }
112  catch (std::exception&amp; e)
113  {
114    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
115  }
116
117  return 0;
118}
119</pre>
120<p>
121          Return to <a class="link" href="../tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">Daytime.6 - An
122          asynchronous UDP daytime server</a>
123        </p>
124</div>
125<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
126<td align="left"></td>
127<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
128      Kohlhoff<p>
129        Distributed under the Boost Software License, Version 1.0. (See accompanying
130        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>)
131      </p>
132</div></td>
133</tr></table>
134<hr>
135<div class="spirit-nav">
136<a accesskey="p" href="../tutdaytime6.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime6.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.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
137</div>
138</body>
139</html>
140