Unsolved How to bind QTcpSocket to a certain QHostAddress in Qt 4.8.7?
-
Using Qt 5.12.1, we are currently using
QTcpSocket::bind()
(see [1]) to bind aQTcpSocket
to a certainQHostAddress
(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 forQUdpSocket
(see [2] and [3]).How can we bind a
QTcpSocket
to a specificQHostAddress
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 -
@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.
-
@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 ;-)
-
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#772After you
bind()
you can pass the descriptor to the Qt API with QAbstractSocket::setSocketDescriptor. -
@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.
-
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 thebind()
will fail?Or am I misunderstanding things here?
-
@Bart_Vandewoestyne
If you read the link I showed you, you'd see that that connect is done via::connect()
, the C++/Windows/Linuxconnect
not the Qtsocket->connectToHost()
. Then Qtsocket.setSocketDescriptor(desc)
is performed, by which time the socket is connected. -
@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), thenbind()
it, and then callQTcpSocket::setSocketDescriptor
on a default-initialized socket object.