5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
14 #include <boost/array.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/asio.hpp>
19 using boost::asio::ip::udp
;
21 std::string
make_daytime_string()
23 using namespace std
; // For time_t, time and ctime;
31 udp_server(boost::asio::io_context
& io_context
)
32 : socket_(io_context
, udp::endpoint(udp::v4(), 13))
40 socket_
.async_receive_from(
41 boost::asio::buffer(recv_buffer_
), remote_endpoint_
,
42 boost::bind(&udp_server::handle_receive
, this,
43 boost::asio::placeholders::error
,
44 boost::asio::placeholders::bytes_transferred
));
47 void handle_receive(const boost::system::error_code
& error
,
48 std::size_t /*bytes_transferred*/)
52 boost::shared_ptr
<std::string
> message(
53 new std::string(make_daytime_string()));
55 socket_
.async_send_to(boost::asio::buffer(*message
), remote_endpoint_
,
56 boost::bind(&udp_server::handle_send
, this, message
,
57 boost::asio::placeholders::error
,
58 boost::asio::placeholders::bytes_transferred
));
64 void handle_send(boost::shared_ptr
<std::string
> /*message*/,
65 const boost::system::error_code
& /*error*/,
66 std::size_t /*bytes_transferred*/)
71 udp::endpoint remote_endpoint_
;
72 boost::array
<char, 1> recv_buffer_
;
79 boost::asio::io_context io_context
;
80 udp_server
server(io_context
);
83 catch (std::exception
& e
)
85 std::cerr
<< e
.what() << std::endl
;