Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    8
    1434
    Loading More Posts
    • 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
      Bart_Vandewoestyne last edited by

      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

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @Bart_Vandewoestyne last edited by

        @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 Reply Quote 1
        • B
          Bart_Vandewoestyne @kshegunov last edited by

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

          kshegunov JonB 2 Replies Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @Bart_Vandewoestyne last edited by

            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 Reply Quote 5
            • JonB
              JonB @Bart_Vandewoestyne last edited by

              @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 Reply Quote 0
              • B
                Bart_Vandewoestyne last edited by

                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?

                JonB kshegunov 2 Replies Last reply Reply Quote 0
                • JonB
                  JonB @Bart_Vandewoestyne last edited by JonB

                  @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 Reply Quote 0
                  • kshegunov
                    kshegunov Moderators @Bart_Vandewoestyne last edited by kshegunov

                    @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 Reply Quote 1
                    • First post
                      Last post