-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_server.cpp
More file actions
56 lines (52 loc) · 1.61 KB
/
tcp_server.cpp
File metadata and controls
56 lines (52 loc) · 1.61 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
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <set>
const size_t max_length = 1024;
using sock_ptr = std::shared_ptr<boost::asio::ip::tcp::socket>;
std::set<std::shared_ptr<std::thread>> thread_set;
void Seesion(sock_ptr sock) {
try {
for (;;) {
char data[max_length];
memset(data, '\0', max_length);
boost::system::error_code error;
size_t length =
sock->read_some(boost::asio::buffer(data, max_length), error);
if (error == boost::asio::error::eof) {
std::cout << "Connection failed by peer" << std::endl;
break;
} else if (error) {
throw boost::system::error_code(error);
}
std::cout << "Remote IP is"
<< sock->remote_endpoint().address().to_string() << '\n';
std::cout << " Recieve data is" << data << '\n';
boost::asio::write(*sock, boost::asio::buffer(data, length));
}
} catch (std::exception &e) {
std::cout << "the code is" << e.what() << std::endl;
}
}
void Server(boost::asio::io_context &ioc, unsigned short portnum) {
boost::asio::ip::tcp::acceptor accept(
ioc, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), portnum));
for (;;) {
sock_ptr sock(new boost::asio::ip::tcp::socket(ioc));
accept.accept(*sock);
auto t = std::make_shared<std::thread>(Seesion, sock);
thread_set.insert(t);
}
}
int main() {
try {
boost::asio::io_context ioc;
Server(ioc, 10086);
for (auto &it : thread_set) {
it->join();
}
} catch (std::exception &e) {
std::cout << "the code is" << e.what();
}
std::cout << "Hello World!\n";
}