From f4bd8b3d260bb09491ba63c77ab7012b744362ef Mon Sep 17 00:00:00 2001 From: Radoslav Gerganov Date: Fri, 17 May 2024 17:25:44 +0300 Subject: [PATCH] rpc : set SO_REUSEADDR for the server socket (#7320) ref: #7293 --- examples/rpc/rpc-server.cpp | 4 ++++ ggml-rpc.cpp | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/rpc/rpc-server.cpp b/examples/rpc/rpc-server.cpp index 41f377376..7c15d2aa4 100644 --- a/examples/rpc/rpc-server.cpp +++ b/examples/rpc/rpc-server.cpp @@ -56,6 +56,10 @@ static bool rpc_server_params_parse(int argc, char ** argv, rpc_server_params & } else if (arg == "-h" || arg == "--help") { print_usage(argc, argv, params); exit(0); + } else { + fprintf(stderr, "error: unknown argument: %s\n", arg.c_str()); + print_usage(argc, argv, params); + exit(0); } } return true; diff --git a/ggml-rpc.cpp b/ggml-rpc.cpp index ba392009f..4a9bfa52d 100644 --- a/ggml-rpc.cpp +++ b/ggml-rpc.cpp @@ -134,7 +134,13 @@ static bool set_no_delay(sockfd_t sockfd) { int flag = 1; // set TCP_NODELAY to disable Nagle's algorithm int ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)); - return ret >= 0; + return ret == 0; +} + +static bool set_reuse_addr(sockfd_t sockfd) { + int flag = 1; + int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(int)); + return ret == 0; } static std::shared_ptr socket_connect(const char * host, int port) { @@ -181,7 +187,10 @@ static std::shared_ptr create_server_socket(const char * host, int por if (sock == nullptr) { return nullptr; } - + if (!set_reuse_addr(sockfd)) { + fprintf(stderr, "Failed to set SO_REUSEADDR\n"); + return nullptr; + } struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(host);