]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // blocking_tcp_echo_client.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2013 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 <cstdlib> | |
12 | #include <cstring> | |
13 | #include <iostream> | |
14 | ||
15 | #include <boost/asio.hpp> | |
16 | ||
17 | using boost::asio::ip::tcp; | |
18 | ||
19 | enum { | |
20 | max_length = 1024 | |
21 | }; | |
22 | ||
23 | int main( int argc, char* argv[]) { | |
24 | try { | |
25 | if ( 3 != argc) { | |
b32b8144 | 26 | std::cerr << "Usage: publisher <host> <queue>\n"; |
7c673cae FG |
27 | return EXIT_FAILURE; |
28 | } | |
29 | boost::asio::io_service io_service; | |
30 | tcp::resolver resolver( io_service); | |
31 | tcp::resolver::query query( tcp::v4(), argv[1], "9997"); | |
32 | tcp::resolver::iterator iterator = resolver.resolve(query); | |
33 | tcp::socket s( io_service); | |
34 | boost::asio::connect( s, iterator); | |
35 | char msg[max_length]; | |
b32b8144 | 36 | std::string queue( argv[2]); |
7c673cae | 37 | std::memset( msg, '\0', max_length); |
b32b8144 | 38 | std::memcpy( msg, queue.c_str(), queue.size() ); |
7c673cae FG |
39 | boost::asio::write( s, boost::asio::buffer( msg, max_length) ); |
40 | for (;;) { | |
41 | std::cout << "publish: "; | |
42 | char request[max_length]; | |
43 | std::cin.getline( request, max_length); | |
44 | boost::asio::write( s, boost::asio::buffer( request, max_length) ); | |
45 | } | |
46 | return EXIT_SUCCESS; | |
47 | } catch ( std::exception const& e) { | |
48 | std::cerr << "Exception: " << e.what() << "\n"; | |
49 | } | |
50 | ||
51 | return EXIT_FAILURE; | |
52 | } |