-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
67 lines (49 loc) · 1.8 KB
/
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
67
#include <iostream>
#include <thread>
#include <arpa/inet.h>
#include <unistd.h>
#include "multiplexing.h"
constexpr char SERVER_IP[] = "127.0.0.1";
constexpr unsigned short SERVER_PORT = 8080;
constexpr int CLIENT_COUNT = 5;
static std::mutex console_mutex;
void sync_println(const std::string &message) {
std::lock_guard lock(console_mutex);
std::cout << message << std::endl;
}
[[noreturn]] void client_task(const int client_id) {
const int client_socket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address{};
if (client_socket < 0) {
std::cerr << "Socket creation error for client " << client_id << std::endl;
return;
}
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
if (connect(client_socket, reinterpret_cast<sockaddr *>(&server_address), sizeof(server_address)) < 0) {
std::cerr << "Connection failed for client " << client_id << std::endl;
close(client_socket);
return;
}
while (true) {
char buffer[MAX_BUFFER_SIZE];
const std::string message = "Hello from client " + std::to_string(client_id);
send(client_socket, message.c_str(), message.length(), 0);
sync_println("Client " + std::to_string(client_id) + " sent message to server: " + message);
if (read(client_socket, buffer, sizeof(buffer)) > 0) {
sync_println("Client " + std::to_string(client_id) + " received from server: " + buffer);
}
}
}
int main() {
std::vector<std::thread> clients;
clients.reserve(CLIENT_COUNT);
for (int i = 1; i <= CLIENT_COUNT; i++) {
clients.emplace_back(client_task, i);
}
for (auto &client: clients) {
client.join();
}
return 0;
}