• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // client.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 <iostream>
12 #include <boost/array.hpp>
13 #include <boost/asio.hpp>
14 
15 using boost::asio::ip::udp;
16 
main(int argc,char * argv[])17 int main(int argc, char* argv[])
18 {
19   try
20   {
21     if (argc != 2)
22     {
23       std::cerr << "Usage: client <host>" << std::endl;
24       return 1;
25     }
26 
27     boost::asio::io_context io_context;
28 
29     udp::resolver resolver(io_context);
30     udp::endpoint receiver_endpoint =
31       *resolver.resolve(udp::v4(), argv[1], "daytime").begin();
32 
33     udp::socket socket(io_context);
34     socket.open(udp::v4());
35 
36     boost::array<char, 1> send_buf  = {{ 0 }};
37     socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);
38 
39     boost::array<char, 128> recv_buf;
40     udp::endpoint sender_endpoint;
41     size_t len = socket.receive_from(
42         boost::asio::buffer(recv_buf), sender_endpoint);
43 
44     std::cout.write(recv_buf.data(), len);
45   }
46   catch (std::exception& e)
47   {
48     std::cerr << e.what() << std::endl;
49   }
50 
51   return 0;
52 }
53