From: Sven M. Hallberg Date: Wed, 3 Sep 2014 12:50:44 +0000 (+0200) Subject: work around picky SO_REUSEADDR semantics on Windows X-Git-Url: http://git.meshlink.io/?p=catta;a=commitdiff_plain;h=f5e8cf973023b76f479c0db9269d08dd3b1d477d work around picky SO_REUSEADDR semantics on Windows --- 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) {