-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_multi_client.cpp
66 lines (51 loc) · 1.92 KB
/
my_multi_client.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
#include <iostream>
#include <asio.hpp>
#include <memory>
using namespace asio::ip;
#include <thread>
#include <random>
#include <chrono>
using namespace asio::ip;
void client_thread(const std::string& message, std::promise<std::string>& result) {
try {
asio::io_context io_context;
tcp::socket socket(io_context);
tcp::resolver resolver(io_context);
tcp::resolver::results_type endpoints = resolver.resolve("localhost", "8000");
// connect to server
asio::connect(socket, endpoints);
// send message
asio::write(socket, asio::buffer(message));
// get reply
std::array<char, 1024> buffer;
asio::error_code error;
size_t length = socket.read_some(asio::buffer(buffer), error);
if (error == asio::error::eof) {
result.set_value("Connection closed by peer.");
} else if (error) {
throw asio::system_error(error);
}
result.set_value(std::string(buffer.data(), length));
} catch (std::exception& e) {
result.set_exception(std::current_exception());
}
}
int main() {
asio::io_context io_context;
tcp::resolver resolver(io_context);
// get server ip and ports
tcp::resolver::results_type endpoints = resolver.resolve("localhost", "8000");
// generate 5 messages randomly,initiate 5 threads and send message
std::vector<std::string> messages = {"hello1", "world2", "test3", "test4", "test5"};
std::vector<std::promise<std::string>> promises(5);
std::vector<std::future<std::string>> futures;
for (int i = 0; i < 5; ++i) {
futures.emplace_back(promises[i].get_future());
std::thread(client_thread, messages[i], std::ref(promises[i])).detach();
}
// return result when all threads finished
for (int i = 0; i < 5; ++i) {
std::cout << "Received message: " << futures[i].get() << std::endl;
}
return 0;
}