-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
71 lines (61 loc) · 2.5 KB
/
Server.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
69
70
71
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/bind.hpp>
#include <boost/bind/bind.hpp>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
boost::asio::io_context io_context;
using boost::asio::ip::tcp;
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 8888));
void start_accept();
void read_handler(const boost::system::error_code &ec, std::size_t len,
char *buffer, std::shared_ptr<tcp::socket> sock);
void write_handler(const boost::system::error_code &ec, std::size_t len,
std::shared_ptr<tcp::socket> sock);
void write_handler(const boost::system::error_code &ec, std::size_t len,
std::shared_ptr<tcp::socket> sock) {
char *buf = new char[1024];
memset(buf, 0, 1024);
sock->async_read_some(
boost::asio::buffer(buf, 1024),
boost::bind(read_handler, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, buf,
std::shared_ptr<tcp::socket>(sock)));
}
void read_handler(const boost::system::error_code &ec, std::size_t len,
char *buffer, std::shared_ptr<tcp::socket> sock) {
std::cout << "Read " << len << " bytes from the socket\n\n"
<< "Writing back\n\n";
sock->async_write_some(
boost::asio::buffer(buffer, 1024),
boost::bind(write_handler, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
std::shared_ptr<tcp::socket>(sock)));
delete[] buffer;
}
void accept_handler(const boost::system::error_code &ec,
std::shared_ptr<tcp::socket> sock) {
char *buf = new char[1024];
sock->async_read_some(
boost::asio::buffer(buf, 1024),
boost::bind(read_handler, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, buf,
std::shared_ptr<tcp::socket>(sock)));
start_accept();
}
void start_accept() {
std::shared_ptr<tcp::socket> conn(new tcp::socket(io_context));
acceptor.async_accept(
*conn, boost::bind(accept_handler, boost::asio::placeholders::error,
std::shared_ptr<tcp::socket>(conn)));
}
int main(int argc, char *argv[]) {
start_accept();
io_context.run();
return 0;
}