-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
54 lines (43 loc) · 1.39 KB
/
client.cpp
File metadata and controls
54 lines (43 loc) · 1.39 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
#include "message.hpp"
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
void async_read(tcp::socket &socket) {
auto buffer = std::make_shared<boost::asio::streambuf>();
boost::asio::async_read_until(socket, *buffer, "\n",
[&socket, buffer](boost::system::error_code ec, std::size_t length) {
if (!ec) {
std::istream is(buffer.get());
std::string received;
std::getline(is, received);
std::cout << "Server: " << received << std::endl;
async_read(socket);
}
}
);
}
int main(int argc, char* argv[]){
if(argc < 2){
std::cerr << "Provide port too as second argument" << std::endl;
return 1;
}
boost::asio::io_context io_context;
tcp::socket socket(io_context);
tcp::resolver resolver(io_context);
boost::asio::connect(socket, resolver.resolve("127.0.0.1", argv[1]));
async_read(socket);
std::thread t([&io_context, &socket]() {
while (true) {
std::string data;
std::cout << "Enter message: ";
std::getline(std::cin, data);
data += "\n";
boost::asio::post(io_context, [&, data]() {
boost::asio::write(socket, boost::asio::buffer(data));
});
}
});
io_context.run();
t.join();
return 0;
}