• 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.5 - A synchronous UDP daytime server</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="tutdaytime4/src.html" title="Source listing for Daytime.4">
10<link rel="next" href="tutdaytime5/src.html" title="Source listing for Daytime.5">
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/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="tutdaytime5/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.tutdaytime5"></a><a class="link" href="tutdaytime5.html" title="Daytime.5 - A synchronous UDP daytime server">Daytime.5 - A synchronous
28      UDP daytime server</a>
29</h3></div></div></div>
30<p>
31        This tutorial program shows how to use asio to implement a server application
32        with UDP.
33      </p>
34<pre class="programlisting">int main()
35{
36  try
37  {
38    boost::asio::io_context io_context;
39</pre>
40<p>
41        Create an <a class="link" href="../reference/ip__udp/socket.html" title="ip::udp::socket">ip::udp::socket</a>
42        object to receive requests on UDP port 13.
43      </p>
44<pre class="programlisting">    udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
45</pre>
46<p>
47        Wait for a client to initiate contact with us. The remote_endpoint object
48        will be populated by <a class="link" href="../reference/basic_datagram_socket/receive_from.html" title="basic_datagram_socket::receive_from">ip::udp::socket::receive_from()</a>.
49      </p>
50<pre class="programlisting">    for (;;)
51    {
52      boost::array&lt;char, 1&gt; recv_buf;
53      udp::endpoint remote_endpoint;
54      socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint);
55</pre>
56<p>
57        Determine what we are going to send back to the client.
58      </p>
59<pre class="programlisting">      std::string message = make_daytime_string();
60</pre>
61<p>
62        Send the response to the remote_endpoint.
63      </p>
64<pre class="programlisting">      boost::system::error_code ignored_error;
65      socket.send_to(boost::asio::buffer(message),
66          remote_endpoint, 0, ignored_error);
67    }
68  }
69</pre>
70<p>
71        Finally, handle any exceptions.
72      </p>
73<pre class="programlisting">  catch (std::exception&amp; e)
74  {
75    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
76  }
77
78  return 0;
79}
80</pre>
81<p>
82        See the <a class="link" href="tutdaytime5/src.html" title="Source listing for Daytime.5">full source listing</a>
83      </p>
84<p>
85        Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a>
86      </p>
87<p>
88        Previous: <a class="link" href="tutdaytime4.html" title="Daytime.4 - A synchronous UDP daytime client">Daytime.4 - A synchronous
89        UDP daytime client</a>
90      </p>
91<p>
92        Next: <a class="link" href="tutdaytime6.html" title="Daytime.6 - An asynchronous UDP daytime server">Daytime.6 - An asynchronous
93        UDP daytime server</a>
94      </p>
95</div>
96<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
97<td align="left"></td>
98<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
99      Kohlhoff<p>
100        Distributed under the Boost Software License, Version 1.0. (See accompanying
101        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>)
102      </p>
103</div></td>
104</tr></table>
105<hr>
106<div class="spirit-nav">
107<a accesskey="p" href="tutdaytime4/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="tutdaytime5/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
108</div>
109</body>
110</html>
111