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/asio.hpp>
17 using boost::asio::ip::udp;
19 std::string make_daytime_string()
21 using namespace std; // For time_t, time and ctime;
30 boost::asio::io_service io_service;
32 udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));
36 boost::array<char, 1> recv_buf;
37 udp::endpoint remote_endpoint;
38 boost::system::error_code error;
39 socket.receive_from(boost::asio::buffer(recv_buf),
40 remote_endpoint, 0, error);
42 if (error && error != boost::asio::error::message_size)
43 throw boost::system::system_error(error);
45 std::string message = make_daytime_string();
47 boost::system::error_code ignored_error;
48 socket.send_to(boost::asio::buffer(message),
49 remote_endpoint, 0, ignored_error);
52 catch (std::exception& e)
54 std::cerr << e.what() << std::endl;