Browse Source

Enable SO_REUSEADDR by default

Paul-Louis Ageneau 3 years ago
parent
commit
ab42344663
1 changed files with 7 additions and 1 deletions
  1. 7 1
      src/impl/tcpserver.cpp

+ 7 - 1
src/impl/tcpserver.cpp

@@ -130,8 +130,14 @@ void TcpServer::listen(uint16_t port) {
 		if (mSock == INVALID_SOCKET)
 			throw std::runtime_error("TCP server socket creation failed");
 
-		// Listen on both IPv6 and IPv4
+		const sockopt_t enabled = 1;
 		const sockopt_t disabled = 0;
+
+		// Enable REUSEADDR
+		::setsockopt(mSock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&enabled),
+			             sizeof(enabled));
+
+		// Listen on both IPv6 and IPv4
 		if (ai->ai_family == AF_INET6)
 			::setsockopt(mSock, IPPROTO_IPV6, IPV6_V6ONLY,
 			             reinterpret_cast<const char *>(&disabled), sizeof(disabled));