@@ -461,10 +461,11 @@ #endif - (uint16_t)bindToHost: (OFString*)host port: (uint16_t)port { + const int one = 1; union { struct sockaddr_storage storage; struct sockaddr_in in; struct sockaddr_in6 in6; } addr; @@ -499,10 +500,15 @@ if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithClass: [self class] socket: self host: host port: port]; + + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) + @throw [OFSetOptionFailedException + exceptionWithClass: [self class] + stream: self]; if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); close(sock); sock = INVALID_SOCKET; @@ -553,10 +559,15 @@ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithClass: [self class] socket: self host: host port: port]; + + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) + @throw [OFSetOptionFailedException + exceptionWithClass: [self class] + stream: self]; if (bind(sock, (struct sockaddr*)&addr.in, sizeof(addr.in)) == -1) { close(sock); sock = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithClass: [self class]