1 // 2 // stream_client.cpp 3 // ~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 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 <cstring> 12 #include <iostream> 13 #include <boost/asio.hpp> 14 15 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) 16 17 using boost::asio::local::stream_protocol; 18 19 constexpr std::size_t max_length = 1024; 20 main(int argc,char * argv[])21int main(int argc, char* argv[]) 22 { 23 try 24 { 25 if (argc != 2) 26 { 27 std::cerr << "Usage: iostream_client <file>\n"; 28 return 1; 29 } 30 31 stream_protocol::endpoint ep(argv[1]); 32 stream_protocol::iostream s(ep); 33 if (!s) 34 { 35 std::cerr << "Unable to connect: " << s.error().message() << std::endl; 36 return 1; 37 } 38 39 std::cout << "Enter message: "; 40 char request[max_length]; 41 std::cin.getline(request, max_length); 42 size_t length = std::strlen(request); 43 s << request; 44 45 char reply[max_length]; 46 s.read(reply, length); 47 std::cout << "Reply is: "; 48 std::cout.write(reply, length); 49 std::cout << "\n"; 50 } 51 catch (std::exception& e) 52 { 53 std::cerr << "Exception: " << e.what() << "\n"; 54 } 55 56 return 0; 57 } 58 59 #else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) 60 # error Local sockets not available on this platform. 61 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) 62