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"
This commit is contained in:
Lion Kortlepel 2022-05-05 00:51:33 +02:00
parent 376594f00c
commit d50980b10f
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B

View File

@ -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);