rpc : set SO_REUSEADDR for the server socket (#7320)

ref: #7293
This commit is contained in:
Radoslav Gerganov 2024-05-17 17:25:44 +03:00 committed by GitHub
parent 51e9d02599
commit f4bd8b3d26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -56,6 +56,10 @@ static bool rpc_server_params_parse(int argc, char ** argv, rpc_server_params &
} else if (arg == "-h" || arg == "--help") { } else if (arg == "-h" || arg == "--help") {
print_usage(argc, argv, params); print_usage(argc, argv, params);
exit(0); exit(0);
} else {
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
print_usage(argc, argv, params);
exit(0);
} }
} }
return true; return true;

View File

@ -134,7 +134,13 @@ static bool set_no_delay(sockfd_t sockfd) {
int flag = 1; int flag = 1;
// set TCP_NODELAY to disable Nagle's algorithm // set TCP_NODELAY to disable Nagle's algorithm
int ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)); 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_t> socket_connect(const char * host, int port) { static std::shared_ptr<socket_t> socket_connect(const char * host, int port) {
@ -181,7 +187,10 @@ static std::shared_ptr<socket_t> create_server_socket(const char * host, int por
if (sock == nullptr) { if (sock == nullptr) {
return nullptr; return nullptr;
} }
if (!set_reuse_addr(sockfd)) {
fprintf(stderr, "Failed to set SO_REUSEADDR\n");
return nullptr;
}
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(host); serv_addr.sin_addr.s_addr = inet_addr(host);