• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Daytime.4 - A synchronous UDP daytime client</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="../tutorial.html" title="Tutorial">
9<link rel="prev" href="tutdaytime3/src.html" title="Source listing for Daytime.3">
10<link rel="next" href="tutdaytime4/src.html" title="Source listing for Daytime.4">
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="tutdaytime3/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tutdaytime4/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_asio.tutorial.tutdaytime4"></a><a class="link" href="tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client">Daytime.4 - A synchronous
28      UDP daytime client</a>
29</h3></div></div></div>
30<p>
31        This tutorial program shows how to use asio to implement a client application
32        with UDP.
33      </p>
34<pre class="programlisting">#include &lt;iostream&gt;
35#include &lt;boost/array.hpp&gt;
36#include &lt;boost/asio.hpp&gt;
37
38using boost::asio::ip::udp;
39</pre>
40<p>
41        The start of the application is essentially the same as for the TCP daytime
42        client.
43      </p>
44<pre class="programlisting">int main(int argc, char* argv[])
45{
46  try
47  {
48    if (argc != 2)
49    {
50      std::cerr &lt;&lt; "Usage: client &lt;host&gt;" &lt;&lt; std::endl;
51      return 1;
52    }
53
54    boost::asio::io_context io_context;
55</pre>
56<p>
57        We use an <a class="link" href="../reference/ip__udp/resolver.html" title="ip::udp::resolver">ip::udp::resolver</a>
58        object to find the correct remote endpoint to use based on the host and service
59        names. The query is restricted to return only IPv4 endpoints by the <a class="link" href="../reference/ip__udp/v4.html" title="ip::udp::v4">ip::udp::v4()</a> argument.
60      </p>
61<pre class="programlisting">    udp::resolver resolver(io_context);
62    udp::endpoint receiver_endpoint =
63      *resolver.resolve(udp::v4(), argv[1], "daytime").begin();
64</pre>
65<p>
66        The <a class="link" href="../reference/ip__basic_resolver/resolve.html" title="ip::basic_resolver::resolve">ip::udp::resolver::resolve()</a>
67        function is guaranteed to return at least one endpoint in the list if it
68        does not fail. This means it is safe to dereference the return value directly.
69      </p>
70<p>
71        Since UDP is datagram-oriented, we will not be using a stream socket. Create
72        an <a class="link" href="../reference/ip__udp/socket.html" title="ip::udp::socket">ip::udp::socket</a>
73        and initiate contact with the remote endpoint.
74      </p>
75<pre class="programlisting">    udp::socket socket(io_context);
76    socket.open(udp::v4());
77
78    boost::array&lt;char, 1&gt; send_buf  = {{ 0 }};
79    socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);
80</pre>
81<p>
82        Now we need to be ready to accept whatever the server sends back to us. The
83        endpoint on our side that receives the server's response will be initialised
84        by <a class="link" href="../reference/basic_datagram_socket/receive_from.html" title="basic_datagram_socket::receive_from">ip::udp::socket::receive_from()</a>.
85      </p>
86<pre class="programlisting">    boost::array&lt;char, 128&gt; recv_buf;
87    udp::endpoint sender_endpoint;
88    size_t len = socket.receive_from(
89        boost::asio::buffer(recv_buf), sender_endpoint);
90
91    std::cout.write(recv_buf.data(), len);
92  }
93</pre>
94<p>
95        Finally, handle any exceptions that may have been thrown.
96      </p>
97<pre class="programlisting">  catch (std::exception&amp; e)
98  {
99    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
100  }
101
102  return 0;
103}
104</pre>
105<p>
106        See the <a class="link" href="tutdaytime4/src.html" title="Source listing for Daytime.4">full source listing</a>
107      </p>
108<p>
109        Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a>
110      </p>
111<p>
112        Previous: <a class="link" href="tutdaytime3.html" title="Daytime.3 - An asynchronous TCP daytime server">Daytime.3 - An
113        asynchronous TCP daytime server</a>
114      </p>
115<p>
116        Next: <a class="link" href="tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">Daytime.5 - A synchronous
117        UDP daytime server</a>
118      </p>
119</div>
120<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
121<td align="left"></td>
122<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
123      Kohlhoff<p>
124        Distributed under the Boost Software License, Version 1.0. (See accompanying
125        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>)
126      </p>
127</div></td>
128</tr></table>
129<hr>
130<div class="spirit-nav">
131<a accesskey="p" href="tutdaytime3/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="tutdaytime4/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
132</div>
133</body>
134</html>
135