-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaytime_client.cpp
More file actions
57 lines (44 loc) · 1.47 KB
/
daytime_client.cpp
File metadata and controls
57 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// daytime_client.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOST_ASIO_NO_DEPRECATED
#include <boost/config.hpp>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
try {
if (argc != 2) {
std::cerr << "Usage: client <host>" << std::endl;
return 0; // NOTE: OK for ctest! CK
}
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
tcp::resolver::results_type endpoints =
resolver.resolve(argv[1], "daytime");
tcp::socket socket(io_context);
boost::asio::connect(socket, endpoints);
for (;;) {
boost::array<char, 128> buf = { 0 };
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
std::cout.write(buf.data(), len);
}
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}