1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Source listing for Daytime.3</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="../tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server"> 9<link rel="prev" href="../tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server"> 10<link rel="next" href="../tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client"> 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="../tutdaytime3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime3.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="../tutdaytime4.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.tutdaytime3.src"></a><a class="link" href="src.html" title="Source listing for Daytime.3">Source listing 28 for Daytime.3</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 <ctime> 41#include <iostream> 42#include <string> 43#include <boost/bind/bind.hpp> 44#include <boost/shared_ptr.hpp> 45#include <boost/enable_shared_from_this.hpp> 46#include <boost/asio.hpp> 47 48using boost::asio::ip::tcp; 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(&now); 55} 56 57class tcp_connection 58 : public boost::enable_shared_from_this<tcp_connection> 59{ 60public: 61 typedef boost::shared_ptr<tcp_connection> pointer; 62 63 static pointer create(boost::asio::io_context& io_context) 64 { 65 return pointer(new tcp_connection(io_context)); 66 } 67 68 tcp::socket& socket() 69 { 70 return socket_; 71 } 72 73 void start() 74 { 75 message_ = make_daytime_string(); 76 77 boost::asio::async_write(socket_, boost::asio::buffer(message_), 78 boost::bind(&tcp_connection::handle_write, shared_from_this(), 79 boost::asio::placeholders::error, 80 boost::asio::placeholders::bytes_transferred)); 81 } 82 83private: 84 tcp_connection(boost::asio::io_context& io_context) 85 : socket_(io_context) 86 { 87 } 88 89 void handle_write(const boost::system::error_code& /*error*/, 90 size_t /*bytes_transferred*/) 91 { 92 } 93 94 tcp::socket socket_; 95 std::string message_; 96}; 97 98class tcp_server 99{ 100public: 101 tcp_server(boost::asio::io_context& io_context) 102 : io_context_(io_context), 103 acceptor_(io_context, tcp::endpoint(tcp::v4(), 13)) 104 { 105 start_accept(); 106 } 107 108private: 109 void start_accept() 110 { 111 tcp_connection::pointer new_connection = 112 tcp_connection::create(io_context_); 113 114 acceptor_.async_accept(new_connection->socket(), 115 boost::bind(&tcp_server::handle_accept, this, new_connection, 116 boost::asio::placeholders::error)); 117 } 118 119 void handle_accept(tcp_connection::pointer new_connection, 120 const boost::system::error_code& error) 121 { 122 if (!error) 123 { 124 new_connection->start(); 125 } 126 127 start_accept(); 128 } 129 130 boost::asio::io_context& io_context_; 131 tcp::acceptor acceptor_; 132}; 133 134int main() 135{ 136 try 137 { 138 boost::asio::io_context io_context; 139 tcp_server server(io_context); 140 io_context.run(); 141 } 142 catch (std::exception& e) 143 { 144 std::cerr << e.what() << std::endl; 145 } 146 147 return 0; 148} 149</pre> 150<p> 151 Return to <a class="link" href="../tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server">Daytime.3 - An 152 asynchronous TCP daytime server</a> 153 </p> 154</div> 155<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 156<td align="left"></td> 157<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 158 Kohlhoff<p> 159 Distributed under the Boost Software License, Version 1.0. (See accompanying 160 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>) 161 </p> 162</div></td> 163</tr></table> 164<hr> 165<div class="spirit-nav"> 166<a accesskey="p" href="../tutdaytime3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime3.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="../tutdaytime4.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 167</div> 168</body> 169</html> 170