multithread-server/client/client.cpp

173 lines
5.8 KiB
C++

#include <iostream>
#include <pthread.h>
#include <arpa/inet.h>
#include <chrono>
#include <thread>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cstring>
// IP address of the server1
std::string SERVER_IP1 = "127.0.0.1";
// Port of the server1
int PORT1 = 9001;
// IP address of the server2
std::string SERVER_IP2 = "127.0.0.1";
// Port of the server2
int PORT2 = 9002;
int client_socket1 = -1;
int client_socket2 = -1;
void* recieve(void* arg) {
int client_socket = *(int*)arg;
delete (int*)arg;
while (true) {
char buffer[128];
ssize_t recieved = recv(client_socket, buffer, sizeof(buffer), 0);
if (recieved == 0 || recieved == -1) {
break;
}
std::cout << buffer << std::endl;
}
return nullptr;
}
void* timer(void* arg) {
int client_socket = *(int*)arg;
delete (int*)arg;
while (true) {
send(client_socket, "", 1, 0);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return nullptr;
}
int main() {
pthread_t t1 = -1, t2 = -1;
std::cout << "Usage:\n\tconnectN - connect to the server\n\tdisconnectN - disconnect from the server\n\tsendN - send request to the server\n\ttimerN - begin polling of the server\n\tstopN - stop polling of the server\n\tquit - quit" << std::endl;
while(true) {
std::string command;
std::cin >> command;
if(command == "quit") {
break;
} else if (command == "connect1") {
client_socket1 = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address1;
server_address1.sin_family = AF_INET;
server_address1.sin_port = htons(PORT1);
server_address1.sin_addr.s_addr = inet_addr(SERVER_IP1.c_str());
if (connect(client_socket1, (struct sockaddr*)&server_address1, sizeof(server_address1)) == 0) {
std::cout << "Connected to the server 1" << std::endl;
pthread_t tid1;
int* client_socket_copy1 = new int(client_socket1);
pthread_create(&tid1, nullptr, &recieve, client_socket_copy1);
} else {
std::cout << "Failed to connect " << std::strerror(errno) << std::endl;
}
} else if (command == "connect2") {
client_socket2 = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address2;
server_address2.sin_family = AF_INET;
server_address2.sin_port = htons(PORT2);
server_address2.sin_addr.s_addr = inet_addr(SERVER_IP2.c_str());
if (connect(client_socket2, (struct sockaddr*)&server_address2, sizeof(server_address2)) == 0) {
std::cout << "Connected to the server 2" << std::endl;
pthread_t tid2;
int* client_socket_copy2 = new int(client_socket2);
pthread_create(&tid2, nullptr, &recieve, client_socket_copy2);
} else {
std::cout << "Failed to connect " << std::strerror(errno) << std::endl;
}
} else if (command == "disconnect1") {
if (client_socket1 != -1) {
close(client_socket1);
client_socket1 = -1;
std::cout << "Disconnected" << std::endl;
} else {
std::cout << "Server is not connected" << std::endl;
}
} else if (command == "disconnect2") {
if (client_socket2 != -1) {
close(client_socket1);
client_socket2 = -1;
std::cout << "Disconnected" << std::endl;
} else {
std::cout << "Server is not connected" << std::endl;
}
} else if (command == "send1") {
if (send(client_socket1, "", 1, 0) == -1) {
std::cout << "Server is not connected" << std::endl;
} else {
std::cout << "Message sent" << std::endl;
}
} else if (command == "send2") {
if (send(client_socket2, "", 1, 0) == -1) {
std::cout << "Server is not connected" << std::endl;
} else {
std::cout << "Message sent" << std::endl;
}
} else if (command == "timer1") {
if (client_socket1 != -1) {
int* client_socket_copy1 = new int(client_socket1);
pthread_create(&t1, nullptr, &timer, client_socket_copy1);
pthread_detach(t1);
std::cout << "Timer created" << std::endl;
} else {
std::cout << "Server is not connected" << std::endl;
}
} else if (command == "timer2") {
if (client_socket1 != -1) {
int* client_socket_copy2 = new int(client_socket2);
pthread_create(&t2, nullptr, &timer, client_socket_copy2);
pthread_detach(t2);
std::cout << "Timer created" << std::endl;
} else {
std::cout << "Server is not connected" << std::endl;
}
} else if (command == "stop1") {
if (t1 != -1) {
pthread_cancel(t1);
t1 = -1;
std::cout << "Timer removed" << std::endl;
} else {
std::cout << "Timer is not running" << std::endl;
}
} else if (command == "stop2") {
if (t2 != -1) {
pthread_cancel(t2);
t2 = -1;
std::cout << "Timer removed" << std::endl;
} else {
std::cout << "Timer is not running" << std::endl;
}
} else {
std::cout << "Unknown command" << std::endl;
}
}
if (t1 != -1) {
pthread_cancel(t1);
}
if (t2 != -1) {
pthread_cancel(t2);
}
close(client_socket1);
close(client_socket2);
return 0;
}