[Networking] Fix "port in use" error (#4772)

This commit is contained in:
Chris Miles 2025-03-12 00:52:17 -05:00 committed by GitHub
parent 8203c034bf
commit 9cb72a6ba7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -303,6 +303,14 @@ bool IpUtil::IsPortInUse(const std::string& ip, int port) {
return true; // Assume in use on failure return true; // Assume in use on failure
} }
#ifdef _WIN32
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&opt, sizeof(opt)); // Windows-specific
#else
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // Linux/macOS
#endif
sockaddr_in addr{}; sockaddr_in addr{};
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);