-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
92 lines (88 loc) · 2.08 KB
/
Client.cpp
File metadata and controls
92 lines (88 loc) · 2.08 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
81
82
83
84
85
86
87
88
89
90
91
92
#include <boost/asio.hpp>
#include <algorithm>
#include <iostream>
class client
{
public:
client(boost::asio::io_context& context)
: context(context)
, sock(context)
{
buff.resize(1024, 0);
}
void connect(const std::string& ip, short port)
{
sock.async_connect(
boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address(ip), port),
std::bind(&client::on_connect, this, std::placeholders::_1));
}
~client()
{
context.run();
}
private:
void on_connect(boost::system::error_code error)
{
if(!error)
{
write();
}
else
{
shutdown("On connect: " + error.message());
}
}
void write()
{
std::cout << "Write message: ";
std::cin >> buff;
buff.resize(1024);
sock.async_write_some(boost::asio::buffer(buff.data(), 1024),
std::bind(&client::on_write, this, std::placeholders::_1, std::placeholders::_2));
}
void on_write(boost::system::error_code error, std::size_t bytes)
{
if(!error)
{
read();
}
else
{
shutdown("On write: " + error.message());
}
}
void read()
{
buff.resize(1024, 0);
sock.async_read_some(boost::asio::buffer(buff.data(), 1024),
std::bind(&client::on_read, this, std::placeholders::_1, std::placeholders::_2));
}
void on_read(boost::system::error_code error, std::size_t bytes)
{
if(!error)
{
std::cout << "From server: " << buff << std::endl;
write();
}
else
{
shutdown("On read: " + error.message());
}
}
void shutdown(const std::string& what)
{
std::cout << what << std::endl;
sock.close();
exit(3);
}
private:
boost::asio::io_context& context;
boost::asio::ip::tcp::socket sock;
std::string buff;
};
int main()
{
boost::asio::io_context ctx;
client cl(ctx);
cl.connect("127.0.0.1", 55001);
}