From d50980b10f0e3965fdf518b580cf5690760c908a Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 5 May 2022 00:51:33 +0200 Subject: [PATCH] Use SO_DONTLINGER to fix bind() address in use error This will ensure the socket does not linger, which fixes the common and *super* annoying issue of "bind(): address already in use" --- src/Network/Server.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Network/Server.cpp b/src/Network/Server.cpp index 28302aa..74adc61 100644 --- a/src/Network/Server.cpp +++ b/src/Network/Server.cpp @@ -33,6 +33,11 @@ void Server::TCPClientMain() { LOG(ERROR) << "Socket failed! Error code: " << WSAGetLastError(); return; } + const char optval = 0; + int status = ::setsockopt(TCPSocket, SOL_SOCKET, SO_DONTLINGER, &optval, sizeof(optval)); + if (status < 0) { + LOG(INFO) << "Failed to set DONTLINGER: " << GetSocketApiError(); + } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(Port); inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);