]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // server.cpp | |
3 | // ~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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/asio.hpp> | |
15 | ||
16 | using boost::asio::ip::tcp; | |
17 | ||
18 | std::string make_daytime_string() | |
19 | { | |
20 | using namespace std; // For time_t, time and ctime; | |
21 | time_t now = time(0); | |
22 | return ctime(&now); | |
23 | } | |
24 | ||
25 | int main() | |
26 | { | |
27 | try | |
28 | { | |
b32b8144 | 29 | boost::asio::io_context io_context; |
7c673cae | 30 | |
b32b8144 | 31 | tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13)); |
7c673cae FG |
32 | |
33 | for (;;) | |
34 | { | |
b32b8144 | 35 | tcp::socket socket(io_context); |
7c673cae FG |
36 | acceptor.accept(socket); |
37 | ||
38 | std::string message = make_daytime_string(); | |
39 | ||
40 | boost::system::error_code ignored_error; | |
41 | boost::asio::write(socket, boost::asio::buffer(message), ignored_error); | |
42 | } | |
43 | } | |
44 | catch (std::exception& e) | |
45 | { | |
46 | std::cerr << e.what() << std::endl; | |
47 | } | |
48 | ||
49 | return 0; | |
50 | } |