#include #include #include #include #include #include #include #include #include // IP address of the server1 std::string SERVER_IP1 = "94.250.253.128"; // 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) { std::cout << "Server shutted down" << std::endl; close(client_socket); if (client_socket1 == client_socket) { client_socket1 = -1; } if (client_socket2 == client_socket) { client_socket2 = -1; } break; } if (recieved == -1) { break; } std::cout << buffer << std::endl; } return nullptr; } struct timer_payload { int client_socket; std::string msg; }; void* timer(void* arg) { timer_payload payload = *(timer_payload*)arg; int client_socket = payload.client_socket; std::string msg = payload.msg; while (true) { send(client_socket, msg.c_str(), msg.length(), 0); std::this_thread::sleep_for(std::chrono::seconds(3)); } return nullptr; } int main() { pthread_t t1 = -1, t2 = -1; pthread_t tid1, tid2; 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") { if (client_socket1 != -1) { std::cout << "Server is already connected" << std::endl; continue; } 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; int* client_socket_copy1 = new int(client_socket1); pthread_create(&tid1, nullptr, &recieve, client_socket_copy1); } else { client_socket1 = -1; std::cout << "Failed to connect " << std::strerror(errno) << std::endl; } } else if (command == "connect2") { if (client_socket2 != -1) { std::cout << "Server is already connected" << std::endl; continue; } 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; int* client_socket_copy2 = new int(client_socket2); pthread_create(&tid2, nullptr, &recieve, client_socket_copy2); } else { client_socket2 = -1; std::cout << "Failed to connect " << std::strerror(errno) << std::endl; } } else if (command == "disconnect1") { if (client_socket1 != -1) { close(client_socket1); pthread_cancel(tid1); client_socket1 = -1; std::cout << "Disconnected" << std::endl; if (t1 != -1) { pthread_cancel(t1); t1 = -1; std::cout << "Timer removed" << std::endl; } } else { std::cout << "Server is not connected" << std::endl; } } else if (command == "disconnect2") { if (client_socket2 != -1) { close(client_socket2); pthread_cancel(tid2); client_socket2 = -1; std::cout << "Disconnected" << std::endl; if (t2 != -1) { pthread_cancel(t2); t2 = -1; std::cout << "Timer removed" << std::endl; } } else { std::cout << "Server is not connected" << std::endl; } } else if (command == "send1") { std::cout << "Enter the message: "; std::string msg; std::cin >> msg; if (send(client_socket1, msg.c_str(), msg.length(), 0) == -1) { std::cout << "Server is not connected" << std::endl; } else { std::cout << "Message sent" << std::endl; } } else if (command == "send2") { std::cout << "Enter the message: "; std::string msg; std::cin >> msg; if (send(client_socket2, msg.c_str(), msg.length(), 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) { std::cout << "Enter the message: "; std::string msg; std::cin >> msg; struct timer_payload p; p.client_socket = client_socket1; p.msg = msg; pthread_create(&t1, nullptr, &timer, &p); pthread_detach(t1); std::cout << "Timer created" << std::endl; } else { std::cout << "Server is not connected" << std::endl; } } else if (command == "timer2") { if (client_socket2 != -1) { std::cout << "Enter the message: "; std::string msg; std::cin >> msg; struct timer_payload p; p.client_socket = client_socket2; p.msg = msg; pthread_create(&t2, nullptr, &timer, &p); 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; }