Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (58 loc) · 1.85 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <chrono>
#include <boost/asio.hpp>
#include <boost/asio/ts/buffer.hpp>
#include <boost/asio/ts/internet.hpp>
namespace asio = boost::asio;
using boost::system::error_code;
//global 20 kb buffer
std::vector<char> vbuffer(20*1024);
void grabsomedata(asio::ip::tcp::socket& socket) {
socket.async_read_some(asio::buffer(vbuffer.data(),vbuffer.size()),
[&](std::error_code ec ,std::size_t length) {
if (!ec) {
std::cout << "\n\n read ::" << length << " bytes" << std::endl;
for (int i=0;i<length;i++) {
std::cout << vbuffer[i];
}
grabsomedata(socket);
}
}
);
}
int main() {
error_code ec;
asio::io_context context;
//fake work
auto work = asio::make_work_guard(context);
std::thread fthread=std::thread([&]() {
context.run();
});
//address
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("54.84.58.92",ec),80);
//acts as a doorways b/w networks
asio::ip::tcp::socket socket(context);
//tell socket to connect
socket.connect(endpoint,ec);
if (!ec) {
std::cout << "connected successfully" << std::endl;
}
else {
std::cout << "failed to connect" << ec.message() << std::endl;
}
if (socket.is_open()) {
grabsomedata(socket);
std::string sRequest =
"GET /html HTTP/1.1\r\n"
"Host: httpbin.org\r\n"
"Connection: close\r\n\r\n";
socket.write_some(asio::buffer(sRequest.data(),sRequest.size()),ec);
using namespace std::chrono_literals;
std::this_thread::sleep_for(20000ms);
context.stop();
if (fthread.joinable()) {
fthread.join();
}
}
return 0;
}