]>
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/array.hpp> | |
15 | #include <boost/asio.hpp> | |
16 | ||
17 | using boost::asio::ip::udp; | |
18 | ||
19 | std::string make_daytime_string() | |
20 | { | |
21 | using namespace std; // For time_t, time and ctime; | |
22 | time_t now = time(0); | |
23 | return ctime(&now); | |
24 | } | |
25 | ||
26 | int main() | |
27 | { | |
28 | try | |
29 | { | |
b32b8144 | 30 | boost::asio::io_context io_context; |
7c673cae | 31 | |
b32b8144 | 32 | udp::socket socket(io_context, udp::endpoint(udp::v4(), 13)); |
7c673cae FG |
33 | |
34 | for (;;) | |
35 | { | |
36 | boost::array<char, 1> recv_buf; | |
37 | udp::endpoint remote_endpoint; | |
b32b8144 | 38 | socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint); |
7c673cae FG |
39 | |
40 | std::string message = make_daytime_string(); | |
41 | ||
42 | boost::system::error_code ignored_error; | |
43 | socket.send_to(boost::asio::buffer(message), | |
44 | remote_endpoint, 0, ignored_error); | |
45 | } | |
46 | } | |
47 | catch (std::exception& e) | |
48 | { | |
49 | std::cerr << e.what() << std::endl; | |
50 | } | |
51 | ||
52 | return 0; | |
53 | } |