From f5e8cf973023b76f479c0db9269d08dd3b1d477d Mon Sep 17 00:00:00 2001 From: "Sven M. Hallberg" Date: Wed, 3 Sep 2014 14:50:44 +0200 Subject: [PATCH] work around picky SO_REUSEADDR semantics on Windows --- src/socket.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/socket.c b/src/socket.c index cfb3b7d..4145899 100644 --- a/src/socket.c +++ b/src/socket.c @@ -191,6 +191,16 @@ static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { assert(sa); assert(l > 0); +#ifdef _WIN32 + // Windows does not allow address reuse when SO_REUSEADDR was set after + // bind() on the first socket, so we must set it before. + // Note that this spoils the detection trickery below and the warning will + // not be logged. + + if (reuseaddr(fd) < 0) + return -1; +#endif + if (bind(fd, sa, l) < 0) { if (errno != EADDRINUSE) { -- 2.39.5