]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp11/local/stream_client.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / local / stream_client.cpp
CommitLineData
b32b8144
FG
1//
2// stream_client.cpp
3// ~~~~~~~~~~~~~~~~~
4//
92f5a8d4 5// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
b32b8144
FG
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9//
10
11#include <cstdlib>
12#include <cstring>
13#include <iostream>
14#include <boost/asio.hpp>
15
16#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
17
18using boost::asio::local::stream_protocol;
19
20constexpr std::size_t max_length = 1024;
21
22int main(int argc, char* argv[])
23{
24 try
25 {
26 if (argc != 2)
27 {
28 std::cerr << "Usage: stream_client <file>\n";
29 return 1;
30 }
31
32 boost::asio::io_context io_context;
33
34 stream_protocol::socket s(io_context);
35 s.connect(stream_protocol::endpoint(argv[1]));
36
37 std::cout << "Enter message: ";
38 char request[max_length];
39 std::cin.getline(request, max_length);
40 size_t request_length = std::strlen(request);
41 boost::asio::write(s, boost::asio::buffer(request, request_length));
42
43 char reply[max_length];
44 size_t reply_length = boost::asio::read(s,
45 boost::asio::buffer(reply, request_length));
46 std::cout << "Reply is: ";
47 std::cout.write(reply, reply_length);
48 std::cout << "\n";
49 }
50 catch (std::exception& e)
51 {
52 std::cerr << "Exception: " << e.what() << "\n";
53 }
54
55 return 0;
56}
57
58#else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
59# error Local sockets not available on this platform.
60#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)