Closed gaowanlu closed 8 months ago
#include <iostream> #include <string> #include <thread> #include <mutex> #include <vector> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> std::vector<std::pair<int, struct sockaddr_in>> clients; std::mutex clients_mutex; void handle_client(int client_socket, struct sockaddr_in client_addr) { char buffer[1024]; ssize_t bytes_received; while ((bytes_received = recv(client_socket, buffer, sizeof(buffer), 0)) > 0) { std::string message(buffer, bytes_received); std::cout << "Received message from client: " << message << std::endl; // Send message back to client send(client_socket, message.c_str(), message.length(), 0); } close(client_socket); // Remove client from clients list std::unique_lock<std::mutex> lock(clients_mutex); for (auto it = clients.begin(); it != clients.end(); ++it) { if (it->first == client_socket) { clients.erase(it); break; } } } int main() { int server_socket = socket(AF_INET, SOCK_DGRAM, 0); if (server_socket == -1) { std::cerr << "Failed to create socket" << std::endl; return 1; } struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(54000); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { std::cerr << "Failed to bind socket" << std::endl; return 1; } std::cout << "Server listening on port 54000" << std::endl; while (true) { char buffer[1024]; struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); ssize_t bytes_received = recvfrom(server_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_addr_len); if (bytes_received > 0) { std::string message(buffer, bytes_received); std::cout << "Received message from client: " << message << std::endl; // Add client to clients list std::unique_lock<std::mutex> lock(clients_mutex); clients.push_back(std::make_pair(socket(AF_INET, SOCK_DGRAM, 0), client_addr)); // Start thread to handle client std::thread handle_client_thread(handle_client, clients.back().first, clients.back().second); handle_client_thread.detach(); } } close(server_socket); return 0; }
#include <iostream> #include <string> #include <thread> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main() { int client_socket = socket(AF_INET, SOCK_DGRAM, 0); if (client_socket == -1) { std::cerr << "Failed to create socket" << std::endl; return 1; } struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(54000); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); std::string message; while (true) { std::cout << "Enter a message to send to the server: "; std::getline(std::cin, message); if (sendto(client_socket, message.c_str(), message.length(), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { std::cerr << "Failed to send message" << std::endl; continue; } char buffer[1024]; socklen_t server_addr_len = sizeof(server_addr); ssize_t bytes_received = recvfrom(client_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&server_addr, &server_addr_len); if (bytes_received > 0) { std::string response(buffer, bytes_received); std::cout << "Received response from server: " << response << std::endl; } } close(client_socket); return 0; }
https://github.com/crust-hub/tubekit/commit/808a58f69760b7e2fa1fa6eba6044631e6c0e8a3