MCPcopy Create free account
hub / github.com/boostorg/asio / main

Function main

example/cpp11/socks4/sync_client.cpp:21–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19using boost::asio::ip::tcp;
20
21int 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_context io_context;
35
36 // Get a list of endpoints corresponding to the SOCKS 4 server name.
37 tcp::resolver resolver(io_context);
38 auto endpoints = resolver.resolve(argv[1], argv[2]);
39
40 // Try each endpoint until we successfully establish a connection to the
41 // SOCKS 4 server.
42 tcp::socket socket(io_context);
43 boost::asio::connect(socket, endpoints);
44
45 // Get an endpoint for the Boost website. This will be passed to the SOCKS
46 // 4 server. Explicitly specify IPv4 since SOCKS 4 does not support IPv6.
47 auto http_endpoint =
48 *resolver.resolve(tcp::v4(), "www.boost.org", "http").begin();
49
50 // Send the request to the SOCKS 4 server.
51 socks4::request socks_request(
52 socks4::request::connect, http_endpoint, argv[3]);
53 boost::asio::write(socket, socks_request.buffers());
54
55 // Receive a response from the SOCKS 4 server.
56 socks4::reply socks_reply;
57 boost::asio::read(socket, socks_reply.buffers());
58
59 // Check whether we successfully negotiated with the SOCKS 4 server.
60 if (!socks_reply.success())
61 {
62 std::cout << "Connection failed.\n";
63 std::cout << "status = 0x" << std::hex << socks_reply.status();
64 return 1;
65 }
66
67 // Form the HTTP request. We specify the "Connection: close" header so that
68 // the server will close the socket after transmitting the response. This
69 // will allow us to treat all data up until the EOF as the response.
70 std::string request =
71 "GET / HTTP/1.0\r\n"
72 "Host: www.boost.org\r\n"
73 "Accept: */*\r\n"
74 "Connection: close\r\n\r\n";
75
76 // Send the HTTP request.
77 boost::asio::write(socket, boost::asio::buffer(request));
78

Callers

nothing calls this directly

Calls 12

writeFunction · 0.85
readFunction · 0.85
bufferFunction · 0.85
successMethod · 0.80
statusMethod · 0.80
connectFunction · 0.50
resolveMethod · 0.45
beginMethod · 0.45
buffersMethod · 0.45
read_someMethod · 0.45
writeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected