Imported Upstream version 1.57.0
[platform/upstream/boost.git] / doc / html / boost_asio / example / cpp03 / socks4 / sync_client.cpp
1 //
2 // sync_client.cpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 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 <iostream>
12 #include <iomanip>
13 #include <ostream>
14 #include <string>
15 #include <boost/asio.hpp>
16 #include <boost/array.hpp>
17 #include "socks4.hpp"
18
19 using boost::asio::ip::tcp;
20
21 int main(int argc, char* argv[])
22 {
23   try
24   {
25     if (argc != 4)
26     {
27       std::cout << "Usage: sync_client <socks4server> <socks4port> <user>\n";
28       std::cout << "Examples:\n";
29       std::cout << "  sync_client 127.0.0.1 1080 chris\n";
30       std::cout << "  sync_client localhost socks chris\n";
31       return 1;
32     }
33
34     boost::asio::io_service io_service;
35
36     // Get a list of endpoints corresponding to the SOCKS 4 server name.
37     tcp::resolver resolver(io_service);
38     tcp::resolver::query socks_query(argv[1], argv[2]);
39     tcp::resolver::iterator endpoint_iterator = resolver.resolve(socks_query);
40
41     // Try each endpoint until we successfully establish a connection to the
42     // SOCKS 4 server.
43     tcp::socket socket(io_service);
44     boost::asio::connect(socket, endpoint_iterator);
45
46     // Get an endpoint for the Boost website. This will be passed to the SOCKS
47     // 4 server. Explicitly specify IPv4 since SOCKS 4 does not support IPv6.
48     tcp::resolver::query http_query(tcp::v4(), "www.boost.org", "http");
49     tcp::endpoint http_endpoint = *resolver.resolve(http_query);
50
51     // Send the request to the SOCKS 4 server.
52     socks4::request socks_request(
53         socks4::request::connect, http_endpoint, argv[3]);
54     boost::asio::write(socket, socks_request.buffers());
55
56     // Receive a response from the SOCKS 4 server.
57     socks4::reply socks_reply;
58     boost::asio::read(socket, socks_reply.buffers());
59
60     // Check whether we successfully negotiated with the SOCKS 4 server.
61     if (!socks_reply.success())
62     {
63       std::cout << "Connection failed.\n";
64       std::cout << "status = 0x" << std::hex << socks_reply.status();
65       return 1;
66     }
67
68     // Form the HTTP request. We specify the "Connection: close" header so that
69     // the server will close the socket after transmitting the response. This
70     // will allow us to treat all data up until the EOF as the response.
71     std::string request =
72       "GET / HTTP/1.0\r\n"
73       "Host: www.boost.org\r\n"
74       "Accept: */*\r\n"
75       "Connection: close\r\n\r\n";
76
77     // Send the HTTP request.
78     boost::asio::write(socket, boost::asio::buffer(request));
79
80     // Read until EOF, writing data to output as we go.
81     boost::array<char, 512> response;
82     boost::system::error_code error;
83     while (std::size_t s = socket.read_some(
84           boost::asio::buffer(response), error))
85       std::cout.write(response.data(), s);
86     if (error != boost::asio::error::eof)
87       throw boost::system::system_error(error);
88   }
89   catch (std::exception& e)
90   {
91     std::cout << "Exception: " << e.what() << "\n";
92   }
93
94   return 0;
95 }