-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (43 loc) · 1.65 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <boost/asio.hpp>
using namespace std;
std::vector<char> virtual_buffer(20*1024); // pool of memory where read data from a source is kept
void data_2_grab(boost::asio::ip::tcp::socket& socket){
socket.async_read_some(boost::asio::buffer(virtual_buffer.data(),virtual_buffer.size()),[&](boost::system::error_code error_pool,std::size_t length){
if(!error_pool){
for(int i=0;i<length;i++){
cout<<virtual_buffer[i]<<endl;
};
data_2_grab(socket);
};
});
};
//above is a function which reads data async from a socket connection. and this operation is run on the context io_context
int main(){
boost::system::error_code error_pool;
boost::asio::io_context context;
boost::asio::io_context::work idleWork(context);
std::thread io_context_thread=std::thread([&](){context.run();});
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::make_address("127.0.0.1",error_pool),8080);
boost::asio::ip::tcp::socket socket(context);
socket.connect(endpoint,error_pool);
if(!error_pool){
cout<<"connected...."<<endl;
} else{
cout<<"there was an error connecting to the address- \n "<<error_pool.message()<<endl;
};
if(socket.is_open()){
data_2_grab(socket);
std::string request="GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
socket.write_some(boost::asio::buffer(request.data(),request.size()),error_pool);
std::this_thread::sleep_for(std::chrono::seconds(5));
context.stop();
if(io_context_thread.joinable()){
io_context_thread.join();
};
};
return 0;
}