-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (41 loc) · 1.59 KB
/
main.cpp
File metadata and controls
49 lines (41 loc) · 1.59 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
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;
int main() {
boost::asio::io_context io_context;
// Create endpoints
std::vector<tcp::endpoint> endpoints;
endpoints.emplace_back(boost::asio::ip::address::from_string("127.0.0.1"), 8000);
endpoints.emplace_back(boost::asio::ip::address::from_string("127.0.0.1"), 8001);
endpoints.emplace_back(boost::asio::ip::address::from_string("127.0.0.1"), 8002);
// Create acceptors for each endpoint
std::vector<tcp::acceptor> acceptors;
acceptors.reserve(endpoints.size());
for (const auto &endpoint: endpoints) {
acceptors.emplace_back(io_context, endpoint);
}
while (true) {
// Wait for a connection on any of the acceptors
tcp::socket socket(io_context);
boost::asio::ip::tcp::acceptor *acceptor = nullptr;
for (auto &a: acceptors) {
if (a.is_open()) {
try {
a.accept(socket);
acceptor = &a;
break;
} catch (const boost::system::system_error &e) {
// Ignore errors and try the next acceptor
}
}
}
if (acceptor) {
char buffer[1024];
size_t bytes_transferred = socket.read_some(boost::asio::buffer(buffer));
std::cout << "Received " << bytes_transferred << " bytes: " << buffer << std::endl;
std::string message = "Hello, client!";
boost::asio::write(socket, boost::asio::buffer(message));
socket.close();
}
}
}