From: Kevin Hock Date: Sat, 11 Feb 2006 23:43:17 +0000 (+0000) Subject: Respect the REUSEADDR flag to allow rebinding; bind to local port prior to connecting... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/c232b3cdc6bd2c2de7731e3fcdc26a376b373533 Respect the REUSEADDR flag to allow rebinding; bind to local port prior to connecting if a local port is specified; add SO_REUSEPORT wherever SO_REUSEADDR is used if SO_REUSEPORT is defined, otherwise reuse fails on BSD systems (e.g. Mac OS X) [ Extension of patch 1415505 ] git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37507 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/unix/gsocket.cpp b/src/unix/gsocket.cpp index 63bed5a32f..8bf9517551 100644 --- a/src/unix/gsocket.cpp +++ b/src/unix/gsocket.cpp @@ -691,7 +691,12 @@ GSocketError GSocket::SetServer() state after being previously closed. */ if (m_reusable) + { setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); +#ifdef SO_REUSEPORT + setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(u_long)); +#endif + } /* Bind to the local address, * retrieve the actual address bound, @@ -890,6 +895,21 @@ GSocketError GSocket::Connect(GSocketStream stream) ioctl(m_fd, FIONBIO, &arg); #endif + // If the reuse flag is set, use the applicable socket reuse flags(s) + if (m_reusable) + { + setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long)); +#ifdef SO_REUSEPORT + setsockopt(m_fd, SOL_SOCKET, SO_REUSEPORT, (const char*)&arg, sizeof(u_long)); +#endif + } + + // If a local address has been set, then we need to bind to it before calling connect + if (m_local && m_local->m_addr) + { + bind(m_fd, m_local->m_addr, m_local->m_len); + } + /* Connect it to the peer address, with a timeout (see below) */ ret = connect(m_fd, m_peer->m_addr, m_peer->m_len);