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
This commit is contained in:
Kevin Hock
2006-02-11 23:43:17 +00:00
parent 33d925b0d9
commit c232b3cdc6

View File

@@ -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);