]> git.saurik.com Git - wxWidgets.git/commitdiff
Respect the REUSEADDR flag to allow rebinding; bind to local port prior to connecting...
authorKevin Hock <hockkn@yahoo.com>
Sat, 11 Feb 2006 23:43:17 +0000 (23:43 +0000)
committerKevin Hock <hockkn@yahoo.com>
Sat, 11 Feb 2006 23:43:17 +0000 (23:43 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37507 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/unix/gsocket.cpp

index 63bed5a32f89d0dc6dc97ab9443896c8d881b40c..8bf9517551c363ed8cdc1ef14ba2dc6111cdf8ca 100644 (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);