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 bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?

How to bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.1k 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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    Using Qt 5.12.1, we are currently using QTcpSocket::bind() (see [1]) to bind a QTcpSocket to a certain QHostAddress (to specify which interface to use for an outgoing connection). Now, we need to backport this piece of code to an older release branch where we have only Qt 4.8.7 available. However, QTcpSocket::bind() does not exist in Qt 4.8.7, it only seems to exist for QUdpSocket (see [2] and [3]).

    How can we bind a QTcpSocket to a specific QHostAddress in Qt 4.8.7?

    [1] https://doc.qt.io/qt-5/qabstractsocket.html#bind
    [2] https://doc.qt.io/archives/qt-4.8/qudpsocket.html#bind
    [3] https://doc.qt.io/archives/qt-4.8/qtcpsocket-members.html

    kshegunovK 1 Reply Last reply
    0
    • B Bart_Vandewoestyne

      Using Qt 5.12.1, we are currently using QTcpSocket::bind() (see [1]) to bind a QTcpSocket to a certain QHostAddress (to specify which interface to use for an outgoing connection). Now, we need to backport this piece of code to an older release branch where we have only Qt 4.8.7 available. However, QTcpSocket::bind() does not exist in Qt 4.8.7, it only seems to exist for QUdpSocket (see [2] and [3]).

      How can we bind a QTcpSocket to a specific QHostAddress in Qt 4.8.7?

      [1] https://doc.qt.io/qt-5/qabstractsocket.html#bind
      [2] https://doc.qt.io/archives/qt-4.8/qudpsocket.html#bind
      [3] https://doc.qt.io/archives/qt-4.8/qtcpsocket-members.html

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Bart_Vandewoestyne said in How to bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?:

      How can we bind a QTcpSocket to a specific QHostAddress in Qt 4.8.7?

      You mean on the client side? I'm not convinced it's possible with 4.8's API. You may need to open the socket natively, bind it and then pass the descriptor on to the Qt API.

      Read and abide by the Qt Code of Conduct

      B 1 Reply Last reply
      1
      • kshegunovK kshegunov

        @Bart_Vandewoestyne said in How to bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?:

        How can we bind a QTcpSocket to a specific QHostAddress in Qt 4.8.7?

        You mean on the client side? I'm not convinced it's possible with 4.8's API. You may need to open the socket natively, bind it and then pass the descriptor on to the Qt API.

        B Offline
        B Offline
        Bart_Vandewoestyne
        wrote on last edited by
        #3

        @kshegunov Yes, client side. Assuming we are on Windows, can you point me in the right direction on how to "open the socket natively and bind it, and then pass the descriptor on to the Qt API"? (I will Google myself too, but having people pointing me in the right direction might speed up my search ;-)

        kshegunovK JonBJ 2 Replies Last reply
        0
        • B Bart_Vandewoestyne

          @kshegunov Yes, client side. Assuming we are on Windows, can you point me in the right direction on how to "open the socket natively and bind it, and then pass the descriptor on to the Qt API"? (I will Google myself too, but having people pointing me in the right direction might speed up my search ;-)

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          I think this is a good starting point:
          https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-bind
          And perhaps Qt5's own source can be of use (as inspiration):
          https://code.woboq.org/qt5/qtbase/src/network/socket/qnativesocketengine_win.cpp.html#772

          After you bind() you can pass the descriptor to the Qt API with QAbstractSocket::setSocketDescriptor.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          5
          • B Bart_Vandewoestyne

            @kshegunov Yes, client side. Assuming we are on Windows, can you point me in the right direction on how to "open the socket natively and bind it, and then pass the descriptor on to the Qt API"? (I will Google myself too, but having people pointing me in the right direction might speed up my search ;-)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Bart_Vandewoestyne
            Yes, you need (in Qt4) to do @kshegunov 's way (of course!).

            There have been questions about this before in this forum, IIRC. Have a look through https://forum.qt.io/topic/32596/set-a-local-port-to-a-qtcpsocket. It's a bit rambling/shambolic, but should give you gist of code needed.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bart_Vandewoestyne
              wrote on last edited by
              #6

              Hmm... for as far as I understand now, I would be doing something like the following:

              const auto desc = socket.socketDescriptor();
              ... bind socket to certain host address using windows API (= modify desc) ...
              socket.setSocketDescriptor(desc);
              socket->connectToHost(ipAddress, port);
              

              but note that on https://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor is written:

              The socket descriptor is not available when QAbstractSocket is in UnconnectedState."

              so from that documentation I would assume that my socket descriptor desc is not available yet at the time I'm trying to bind it to a certain IP address (before I connect)? So the bind() will fail?

              Or am I misunderstanding things here?

              JonBJ kshegunovK 2 Replies Last reply
              0
              • B Bart_Vandewoestyne

                Hmm... for as far as I understand now, I would be doing something like the following:

                const auto desc = socket.socketDescriptor();
                ... bind socket to certain host address using windows API (= modify desc) ...
                socket.setSocketDescriptor(desc);
                socket->connectToHost(ipAddress, port);
                

                but note that on https://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor is written:

                The socket descriptor is not available when QAbstractSocket is in UnconnectedState."

                so from that documentation I would assume that my socket descriptor desc is not available yet at the time I'm trying to bind it to a certain IP address (before I connect)? So the bind() will fail?

                Or am I misunderstanding things here?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Bart_Vandewoestyne
                If you read the link I showed you, you'd see that that connect is done via ::connect(), the C++/Windows/Linux connect not the Qt socket->connectToHost(). Then Qt socket.setSocketDescriptor(desc) is performed, by which time the socket is connected.

                1 Reply Last reply
                0
                • B Bart_Vandewoestyne

                  Hmm... for as far as I understand now, I would be doing something like the following:

                  const auto desc = socket.socketDescriptor();
                  ... bind socket to certain host address using windows API (= modify desc) ...
                  socket.setSocketDescriptor(desc);
                  socket->connectToHost(ipAddress, port);
                  

                  but note that on https://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor is written:

                  The socket descriptor is not available when QAbstractSocket is in UnconnectedState."

                  so from that documentation I would assume that my socket descriptor desc is not available yet at the time I'm trying to bind it to a certain IP address (before I connect)? So the bind() will fail?

                  Or am I misunderstanding things here?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @Bart_Vandewoestyne said in How to bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?:

                  Or am I misunderstanding things here?

                  Indeed. You should create your socket natively (see the example at the end of the bind() docs page), then bind() it, and then call QTcpSocket::setSocketDescriptor on a default-initialized socket object.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1

                  • Login

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