1 //
2 // server.cpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <ctime>
12 #include <iostream>
13 #include <string>
14 #include <boost/array.hpp>
15 #include <boost/bind/bind.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18 #include <boost/asio.hpp>
19
20 using boost::asio::ip::tcp;
21 using boost::asio::ip::udp;
22
make_daytime_string()23 std::string make_daytime_string()
24 {
25 using namespace std; // For time_t, time and ctime;
26 time_t now = time(0);
27 return ctime(&now);
28 }
29
30 class tcp_connection
31 : public boost::enable_shared_from_this<tcp_connection>
32 {
33 public:
34 typedef boost::shared_ptr<tcp_connection> pointer;
35
create(boost::asio::io_context & io_context)36 static pointer create(boost::asio::io_context& io_context)
37 {
38 return pointer(new tcp_connection(io_context));
39 }
40
socket()41 tcp::socket& socket()
42 {
43 return socket_;
44 }
45
start()46 void start()
47 {
48 message_ = make_daytime_string();
49
50 boost::asio::async_write(socket_, boost::asio::buffer(message_),
51 boost::bind(&tcp_connection::handle_write, shared_from_this()));
52 }
53
54 private:
tcp_connection(boost::asio::io_context & io_context)55 tcp_connection(boost::asio::io_context& io_context)
56 : socket_(io_context)
57 {
58 }
59
handle_write()60 void handle_write()
61 {
62 }
63
64 tcp::socket socket_;
65 std::string message_;
66 };
67
68 class tcp_server
69 {
70 public:
tcp_server(boost::asio::io_context & io_context)71 tcp_server(boost::asio::io_context& io_context)
72 : io_context_(io_context),
73 acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
74 {
75 start_accept();
76 }
77
78 private:
start_accept()79 void start_accept()
80 {
81 tcp_connection::pointer new_connection =
82 tcp_connection::create(io_context_);
83
84 acceptor_.async_accept(new_connection->socket(),
85 boost::bind(&tcp_server::handle_accept, this, new_connection,
86 boost::asio::placeholders::error));
87 }
88
handle_accept(tcp_connection::pointer new_connection,const boost::system::error_code & error)89 void handle_accept(tcp_connection::pointer new_connection,
90 const boost::system::error_code& error)
91 {
92 if (!error)
93 {
94 new_connection->start();
95 }
96
97 start_accept();
98 }
99
100 boost::asio::io_context& io_context_;
101 tcp::acceptor acceptor_;
102 };
103
104 class udp_server
105 {
106 public:
udp_server(boost::asio::io_context & io_context)107 udp_server(boost::asio::io_context& io_context)
108 : socket_(io_context, udp::endpoint(udp::v4(), 13))
109 {
110 start_receive();
111 }
112
113 private:
start_receive()114 void start_receive()
115 {
116 socket_.async_receive_from(
117 boost::asio::buffer(recv_buffer_), remote_endpoint_,
118 boost::bind(&udp_server::handle_receive, this,
119 boost::asio::placeholders::error));
120 }
121
handle_receive(const boost::system::error_code & error)122 void handle_receive(const boost::system::error_code& error)
123 {
124 if (!error)
125 {
126 boost::shared_ptr<std::string> message(
127 new std::string(make_daytime_string()));
128
129 socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
130 boost::bind(&udp_server::handle_send, this, message));
131
132 start_receive();
133 }
134 }
135
handle_send(boost::shared_ptr<std::string>)136 void handle_send(boost::shared_ptr<std::string> /*message*/)
137 {
138 }
139
140 udp::socket socket_;
141 udp::endpoint remote_endpoint_;
142 boost::array<char, 1> recv_buffer_;
143 };
144
main()145 int main()
146 {
147 try
148 {
149 boost::asio::io_context io_context;
150 tcp_server server1(io_context);
151 udp_server server2(io_context);
152 io_context.run();
153 }
154 catch (std::exception& e)
155 {
156 std::cerr << e.what() << std::endl;
157 }
158
159 return 0;
160 }
161