• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Source listing for Daytime.1</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="../tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client">
9<link rel="prev" href="../tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client">
10<link rel="next" href="../tutdaytime2.html" title="Daytime.2 - A synchronous TCP 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="../tutdaytime1.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime1.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="../tutdaytime2.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.tutdaytime1.src"></a><a class="link" href="src.html" title="Source listing for Daytime.1">Source listing
28        for Daytime.1</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 &lt;iostream&gt;
41#include &lt;boost/array.hpp&gt;
42#include &lt;boost/asio.hpp&gt;
43
44using boost::asio::ip::tcp;
45
46int main(int argc, char* argv[])
47{
48  try
49  {
50    if (argc != 2)
51    {
52      std::cerr &lt;&lt; "Usage: client &lt;host&gt;" &lt;&lt; std::endl;
53      return 1;
54    }
55
56    boost::asio::io_context io_context;
57
58    tcp::resolver resolver(io_context);
59    tcp::resolver::results_type endpoints =
60      resolver.resolve(argv[1], "daytime");
61
62    tcp::socket socket(io_context);
63    boost::asio::connect(socket, endpoints);
64
65    for (;;)
66    {
67      boost::array&lt;char, 128&gt; buf;
68      boost::system::error_code error;
69
70      size_t len = socket.read_some(boost::asio::buffer(buf), error);
71
72      if (error == boost::asio::error::eof)
73        break; // Connection closed cleanly by peer.
74      else if (error)
75        throw boost::system::system_error(error); // Some other error.
76
77      std::cout.write(buf.data(), len);
78    }
79  }
80  catch (std::exception&amp; e)
81  {
82    std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
83  }
84
85  return 0;
86}
87</pre>
88<p>
89          Return to <a class="link" href="../tutdaytime1.html" title="Daytime.1 - A synchronous TCP daytime client">Daytime.1 - A
90          synchronous TCP daytime client</a>
91        </p>
92</div>
93<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
94<td align="left"></td>
95<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
96      Kohlhoff<p>
97        Distributed under the Boost Software License, Version 1.0. (See accompanying
98        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>)
99      </p>
100</div></td>
101</tr></table>
102<hr>
103<div class="spirit-nav">
104<a accesskey="p" href="../tutdaytime1.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutdaytime1.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="../tutdaytime2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
105</div>
106</body>
107</html>
108