diff --git a/src/network/socket.cpp b/src/network/socket.cpp index 112230f00..4c2056da7 100644 --- a/src/network/socket.cpp +++ b/src/network/socket.cpp @@ -87,25 +87,16 @@ bool UDPSocket::init(bool ipv6, bool noExceptions) m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP); if (m_handle < 0) { - if (noExceptions) { + auto msg = std::string("Failed to create socket: ") + + SOCKET_ERR_STR(LAST_SOCKET_ERR()); + verbosestream << msg << std::endl; + if (noExceptions) return false; - } - - throw SocketException(std::string("Failed to create socket: error ") + - SOCKET_ERR_STR(LAST_SOCKET_ERR())); + throw SocketException(msg); } setTimeoutMs(0); - if (m_addr_family == AF_INET6) { - // Allow our socket to accept both IPv4 and IPv6 connections - // required on Windows: - // https://msdn.microsoft.com/en-us/library/windows/desktop/bb513665(v=vs.85).aspx - int value = 0; - setsockopt(m_handle, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&value), sizeof(value)); - } - return true; } @@ -129,6 +120,19 @@ void UDPSocket::Bind(Address addr) throw SocketException(errmsg); } + if (m_addr_family == AF_INET6) { + // Allow our socket to accept both IPv4 and IPv6 connections + // required on Windows: + // + int value = 0; + if (setsockopt(m_handle, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&value), sizeof(value)) != 0) { + auto errmsg = SOCKET_ERR_STR(LAST_SOCKET_ERR()); + errorstream << "Failed to disable V6ONLY: " << errmsg << std::endl; + throw SocketException(errmsg); + } + } + int ret = 0; if (m_addr_family == AF_INET6) {