• 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.5</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="../tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">
9<link rel="prev" href="../tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">
10<link rel="next" href="../tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime 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="../tutdaytime5.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime5.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.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.tutdaytime5.src"></a><a class="link" href="src.html" title="Source listing for Daytime.5">Source listing
28        for Daytime.5</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/asio.hpp&gt;
45
46using boost::asio::ip::udp;
47
48std::string make_daytime_string()
49{
50  using namespace std; // For time_t, time and ctime;
51  time_t now = time(0);
52  return ctime(&amp;now);
53}
54
55int main()
56{
57  try
58  {
59    boost::asio::io_context io_context;
60
61    udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
62
63    for (;;)
64    {
65      boost::array&lt;char, 1&gt; recv_buf;
66      udp::endpoint remote_endpoint;
67      socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint);
68
69      std::string message = make_daytime_string();
70
71      boost::system::error_code ignored_error;
72      socket.send_to(boost::asio::buffer(message),
73          remote_endpoint, 0, ignored_error);
74    }
75  }
76  catch (std::exception&amp; e)
77  {
78    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
79  }
80
81  return 0;
82}
83</pre>
84<p>
85          Return to <a class="link" href="../tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">Daytime.5 - A
86          synchronous UDP daytime server</a>
87        </p>
88</div>
89<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
90<td align="left"></td>
91<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
92      Kohlhoff<p>
93        Distributed under the Boost Software License, Version 1.0. (See accompanying
94        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>)
95      </p>
96</div></td>
97</tr></table>
98<hr>
99<div class="spirit-nav">
100<a accesskey="p" href="../tutdaytime5.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime5.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.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
101</div>
102</body>
103</html>
104