1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Source listing for Daytime.4</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client"> 9<link rel="prev" href="../tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client"> 10<link rel="next" href="../tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../tutdaytime4.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime4.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tutdaytime5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.tutorial.tutdaytime4.src"></a><a class="link" href="src.html" title="Source listing for Daytime.4">Source listing 28 for Daytime.4</a> 29</h4></div></div></div> 30<pre class="programlisting">// 31// client.cpp 32// ~~~~~~~~~~ 33// 34// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) 35// 36// Distributed under the Boost Software License, Version 1.0. (See accompanying 37// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 38// 39 40#include <iostream> 41#include <boost/array.hpp> 42#include <boost/asio.hpp> 43 44using boost::asio::ip::udp; 45 46int main(int argc, char* argv[]) 47{ 48 try 49 { 50 if (argc != 2) 51 { 52 std::cerr << "Usage: client <host>" << std::endl; 53 return 1; 54 } 55 56 boost::asio::io_context io_context; 57 58 udp::resolver resolver(io_context); 59 udp::endpoint receiver_endpoint = 60 *resolver.resolve(udp::v4(), argv[1], "daytime").begin(); 61 62 udp::socket socket(io_context); 63 socket.open(udp::v4()); 64 65 boost::array<char, 1> send_buf = {{ 0 }}; 66 socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint); 67 68 boost::array<char, 128> recv_buf; 69 udp::endpoint sender_endpoint; 70 size_t len = socket.receive_from( 71 boost::asio::buffer(recv_buf), sender_endpoint); 72 73 std::cout.write(recv_buf.data(), len); 74 } 75 catch (std::exception& e) 76 { 77 std::cerr << e.what() << std::endl; 78 } 79 80 return 0; 81} 82</pre> 83<p> 84 Return to <a class="link" href="../tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client">Daytime.4 - A 85 synchronous UDP daytime client</a> 86 </p> 87</div> 88<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 89<td align="left"></td> 90<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 91 Kohlhoff<p> 92 Distributed under the Boost Software License, Version 1.0. (See accompanying 93 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 94 </p> 95</div></td> 96</tr></table> 97<hr> 98<div class="spirit-nav"> 99<a accesskey="p" href="../tutdaytime4.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime4.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../tutdaytime5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 100</div> 101</body> 102</html> 103