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