Ticket #3131: blocking_dccp_echo_client.cpp

File blocking_dccp_echo_client.cpp, 1.8 KB (added by oakad@…, 13 years ago)
Line 
1#include <cstdlib>
2#include <cstring>
3#include <iostream>
4#include <boost/asio.hpp>
5#include <boost/program_options.hpp>
6
7#include "dccp.hpp"
8
9using boost::asio::ip::dccp;
10using namespace std;
11
12int main(int argc, char* argv[])
13{
14 using namespace boost::program_options;
15
16 options_description desc;
17 desc.add_options()
18 ("help", "produce help message")
19 ("host", value<string>()->default_value("localhost"),
20 "Host to connect to")
21 ("port", value<string>()->default_value("7777"),
22 "Service/Port number to connect to")
23 ("sc", value<unsigned int>()->default_value(0x30304953),
24 "Service code to use");
25
26 variables_map vm;
27
28 try {
29 store(parse_command_line(argc, argv, desc), vm);
30 } catch (error &err) {
31 cout << err.what() << endl;
32 return 1;
33 }
34
35 notify(vm);
36
37 if (vm.count("help")) {
38 cout << desc;
39 return 0;
40 }
41
42 cout << vm["host"].as<string>() << endl;
43 cout << vm["port"].as<string>() << endl;
44 cout << vm["sc"].as<unsigned int>() << endl;
45 try {
46 boost::asio::io_service io_service;
47
48 dccp::resolver resolver(io_service);
49 dccp::resolver::query query(dccp::v4(), vm["host"].as<string>(),
50 vm["port"].as<string>());
51 dccp::resolver::iterator iterator = resolver.resolve(query);
52
53 cout << "socket construct" << endl;
54 dccp::socket s(io_service);
55
56 s.connect(dccp::endpoint(*iterator,
57 vm["sc"].as<unsigned int>()));
58
59 boost::asio::dccp_option::cur_mps c_mps;
60 s.get_option(c_mps);
61
62 cout << "Enter message: ";
63 char request[c_mps.value()];
64 cin.getline(request, c_mps.value());
65 size_t request_length = strlen(request);
66 s.send(boost::asio::buffer(request, request_length));
67
68 char reply[c_mps.value()];
69 size_t reply_length = s.receive(boost::asio::buffer(reply, request_length));
70 cout << "Reply is: ";
71 cout.write(reply, reply_length);
72 cout << "\n";
73 } catch (exception& e) {
74 cerr << "Exception: " << e.what() << "\n";
75 }
76
77 return 0;
78}