Skip to content

Commit 9cb72a6

Browse files
authored
[Networking] Fix "port in use" error (#4772)
1 parent 8203c03 commit 9cb72a6

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

common/ip_util.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ bool IpUtil::IsPortInUse(const std::string& ip, int port) {
303303
return true; // Assume in use on failure
304304
}
305305

306+
#ifdef _WIN32
307+
int opt = 1;
308+
setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char*)&opt, sizeof(opt)); // Windows-specific
309+
#else
310+
int opt = 1;
311+
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // Linux/macOS
312+
#endif
313+
306314
sockaddr_in addr{};
307315
addr.sin_family = AF_INET;
308316
addr.sin_port = htons(port);

0 commit comments

Comments
 (0)