diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 450075c..713930c 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -29,7 +29,7 @@ std::vector command(data.begin(), data.end()); } -static void handle_client(const int client_socket) { +void TcpService::handle_client(const int client_socket) { std::vector buffer; char byte; while (true) { @@ -92,7 +92,7 @@ continue; } - std::cout << "accepted connection from " + std::cout << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port) @@ -100,7 +100,11 @@ // 处理连接 _client_sockets.push_back(client_socket_fd); - handle_client(client_socket_fd); + + std::thread client_thread([this, client_socket_fd]() { + this->handle_client(client_socket_fd); + }); + client_thread.join(); } } catch (const std::exception &e) { std::cerr << "exception in tcp_server::start: " << e.what() << std::endl; diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 450075c..713930c 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -29,7 +29,7 @@ std::vector command(data.begin(), data.end()); } -static void handle_client(const int client_socket) { +void TcpService::handle_client(const int client_socket) { std::vector buffer; char byte; while (true) { @@ -92,7 +92,7 @@ continue; } - std::cout << "accepted connection from " + std::cout << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port) @@ -100,7 +100,11 @@ // 处理连接 _client_sockets.push_back(client_socket_fd); - handle_client(client_socket_fd); + + std::thread client_thread([this, client_socket_fd]() { + this->handle_client(client_socket_fd); + }); + client_thread.join(); } } catch (const std::exception &e) { std::cerr << "exception in tcp_server::start: " << e.what() << std::endl; diff --git a/src/tcp_service.hpp b/src/tcp_service.hpp index b2bcc2f..34a941a 100644 --- a/src/tcp_service.hpp +++ b/src/tcp_service.hpp @@ -43,6 +43,8 @@ int _socket_fd = -1; std::vector _client_sockets{}; int gas_value = 0; + + void handle_client(int client_socket); };