• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Daytime.2 - A synchronous TCP daytime 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="tutdaytime1/src.html" title="Source listing for Daytime.1">
10<link rel="next" href="tutdaytime2/src.html" title="Source listing for Daytime.2">
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="tutdaytime1/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="tutdaytime2/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.tutdaytime2"></a><a class="link" href="tutdaytime2.html" title="Daytime.2 - A synchronous TCP daytime server">Daytime.2 - A synchronous
28      TCP daytime server</a>
29</h3></div></div></div>
30<p>
31        This tutorial program shows how to use asio to implement a server application
32        with TCP.
33      </p>
34<pre class="programlisting">#include &lt;ctime&gt;
35#include &lt;iostream&gt;
36#include &lt;string&gt;
37#include &lt;boost/asio.hpp&gt;
38
39using boost::asio::ip::tcp;
40</pre>
41<p>
42        We define the function <code class="computeroutput">make_daytime_string()</code> to create the string
43        to be sent back to the client. This function will be reused in all of our
44        daytime server applications.
45      </p>
46<pre class="programlisting">std::string make_daytime_string()
47{
48  using namespace std; // For time_t, time and ctime;
49  time_t now = time(0);
50  return ctime(&amp;now);
51}
52
53int main()
54{
55  try
56  {
57    boost::asio::io_context io_context;
58</pre>
59<p>
60        A <a class="link" href="../reference/ip__tcp/acceptor.html" title="ip::tcp::acceptor">ip::tcp::acceptor</a>
61        object needs to be created to listen for new connections. It is initialised
62        to listen on TCP port 13, for IP version 4.
63      </p>
64<pre class="programlisting">    tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13));
65</pre>
66<p>
67        This is an iterative server, which means that it will handle one connection
68        at a time. Create a socket that will represent the connection to the client,
69        and then wait for a connection.
70      </p>
71<pre class="programlisting">    for (;;)
72    {
73      tcp::socket socket(io_context);
74      acceptor.accept(socket);
75</pre>
76<p>
77        A client is accessing our service. Determine the current time and transfer
78        this information to the client.
79      </p>
80<pre class="programlisting">      std::string message = make_daytime_string();
81
82      boost::system::error_code ignored_error;
83      boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
84    }
85  }
86</pre>
87<p>
88        Finally, handle any exceptions.
89      </p>
90<pre class="programlisting">  catch (std::exception&amp; e)
91  {
92    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
93  }
94
95  return 0;
96}
97</pre>
98<p>
99        See the <a class="link" href="tutdaytime2/src.html" title="Source listing for Daytime.2">full source listing</a>
100      </p>
101<p>
102        Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a>
103      </p>
104<p>
105        Previous: <a class="link" href="tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client">Daytime.1 - A synchronous
106        TCP daytime client</a>
107      </p>
108<p>
109        Next: <a class="link" href="tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server">Daytime.3 - An asynchronous
110        TCP daytime server</a>
111      </p>
112</div>
113<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
114<td align="left"></td>
115<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
116      Kohlhoff<p>
117        Distributed under the Boost Software License, Version 1.0. (See accompanying
118        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>)
119      </p>
120</div></td>
121</tr></table>
122<hr>
123<div class="spirit-nav">
124<a accesskey="p" href="tutdaytime1/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="tutdaytime2/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
125</div>
126</body>
127</html>
128