Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set SO_REUSEPORT option for QUdpSocket?
QtWS25 Last Chance

How to set SO_REUSEPORT option for QUdpSocket?

Scheduled Pinned Locked Moved Unsolved General and Desktop
networkingudp
5 Posts 5 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sacha_D
    wrote on 14 May 2018, 07:48 last edited by
    #1

    Hello. How to set SO_REUSEPORT option for QUdpSocket?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 14 May 2018, 10:46 last edited by
      #2

      use BindMode and BindFlag in bind(..) method

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • X Offline
        X Offline
        Xando
        wrote on 5 Jun 2018, 17:30 last edited by Xando 6 May 2018, 17:38
        #3

        I could not get this to work using the QUdpSocket::bind() functions and the BindFlag as dheerendra suggested. There doesn't seem to be a BindFlag for reusing ports, at least not in in Qt4 (what I use). I did look quickly at the Qt5 documentation and didn't see any appropriate BindFlag, either.

        So I did it using Berkeley sockets. I'm using Linux, the same code might work with WinSock, if not something trivially similar will. The following code creates a QUdpSocket, provides it with a low-level socket descriptor that has been set for SO_REUSEPORT, then binds it to port 34567:

        QUdpSocket *sock = new QUdpSocket;
        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        int optval = 1;
        setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, 
                      (void *) &optval, sizeof(optval));
        sock->setSocketDescriptor(sockfd, QUdpSocket::UnconnectedState);
        sock->bind(34567, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
        
        
        J D 2 Replies Last reply 7 Jun 2018, 17:49
        2
        • X Xando
          5 Jun 2018, 17:30

          I could not get this to work using the QUdpSocket::bind() functions and the BindFlag as dheerendra suggested. There doesn't seem to be a BindFlag for reusing ports, at least not in in Qt4 (what I use). I did look quickly at the Qt5 documentation and didn't see any appropriate BindFlag, either.

          So I did it using Berkeley sockets. I'm using Linux, the same code might work with WinSock, if not something trivially similar will. The following code creates a QUdpSocket, provides it with a low-level socket descriptor that has been set for SO_REUSEPORT, then binds it to port 34567:

          QUdpSocket *sock = new QUdpSocket;
          int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
          int optval = 1;
          setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, 
                        (void *) &optval, sizeof(optval));
          sock->setSocketDescriptor(sockfd, QUdpSocket::UnconnectedState);
          sock->bind(34567, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
          
          
          J Offline
          J Offline
          JonB
          wrote on 7 Jun 2018, 17:49 last edited by JonB 6 Jul 2018, 17:49
          #4

          @Xando

          https://stackoverflow.com/questions/47268023/how-to-set-so-reuseaddr-on-the-socket-used-by-qtcpserver takes the same approach as you do if required to access flags not available from Qt.

          FYI though, have a careful read of https://bugreports.qt.io/browse/QTBUG-33419. At one point I see:

             case QNativeSocketEngine::AddressReusable:
          #if defined(SO_REUSEPORT)
                  // on OS X, SO_REUSEADDR isn't sufficient to allow multiple binds to the
                  // same port (which is useful for multicast UDP). SO_REUSEPORT is, but
                  // we most definitely do not want to use this for TCP. See QTBUG-6305.
                  if (socketType == QAbstractSocket::UdpSocket)
                      n = SO_REUSEPORT;
                  else
                      n = SO_REUSEADDR;
          #else
                  n = SO_REUSEADDR;
          #endif
          

          implying that code for QNativeSocketEngine::AddressReusable will use SO_REUSEPORT for QAbstractSocket::UdpSocket , provided available at compilation?

          1 Reply Last reply
          0
          • X Xando
            5 Jun 2018, 17:30

            I could not get this to work using the QUdpSocket::bind() functions and the BindFlag as dheerendra suggested. There doesn't seem to be a BindFlag for reusing ports, at least not in in Qt4 (what I use). I did look quickly at the Qt5 documentation and didn't see any appropriate BindFlag, either.

            So I did it using Berkeley sockets. I'm using Linux, the same code might work with WinSock, if not something trivially similar will. The following code creates a QUdpSocket, provides it with a low-level socket descriptor that has been set for SO_REUSEPORT, then binds it to port 34567:

            QUdpSocket *sock = new QUdpSocket;
            int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            int optval = 1;
            setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, 
                          (void *) &optval, sizeof(optval));
            sock->setSocketDescriptor(sockfd, QUdpSocket::UnconnectedState);
            sock->bind(34567, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
            
            
            D Offline
            D Offline
            Developer123
            wrote on 2 Dec 2022, 11:59 last edited by Developer123 12 Feb 2022, 12:00
            #5

            @Xando On windows, this did not work for me, even fixing the socket options to the windows equivalent. What did work was:

                udpSocket = new QUdpSocket(this);
                groupAddress4 = QHostAddress(multicastAddr);
                bool result =  udpSocket->bind(
                    QHostAddress::AnyIPv4, 
                    port, 
                    QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved