PDA

View Full Version : Binding a QTcpSocket to a specific enternet interface



Banfa
1st June 2009, 16:31
QT Version: 4.5.1
Compiler: mingw32-g++.exe (GCC) 3.4.2 (mingw-special) - the version that came with QT
Platform: Currently Windows, but the development has to be portable to Linux (xUbuntu)

My situation is that I am developing an application using QT and I need to set-up several links to external devices. The platform we are using has 2 ethernet interfaces and there appears to be no way to bind a QTcpSocket to a specific local interface when creating a connection.

I am really looking for confirmation of this and that if I really want to bind to a specific interface it appears that my only option is to create a subclass of QTcpSocket that has a method that makes so decidedly hacky and non-forward compatible calls to the underlying socket engine class (in the same way that QUdpSocket binds to an interface).


As I have been writing this post I am getting a growing awareness that actually the platform routing table in the network stack should take care of binding to the correct interface given the destination IP. Or at least it should if the 2 networks were completely separate but that is related to some network architecture problems which is another issue that I need to resolve for this project but really not your problem at all :D

Cheers
Banfa

wysota
1st June 2009, 19:40
Have you tried using QAbstractSocket::setLocalAddress()?

Banfa
2nd June 2009, 11:22
Hmmmm, I had considered that function but the help says


void QAbstractSocket::setLocalAddress ( const QHostAddress & address ) [protected]

Sets the address on the local side of a connection to address.

You can call this function in a subclass of QAbstractSocket to change the return value of the localAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

Note that this function does not bind the local address of the socket prior to a connection (e.g., QUdpSocket::bind()).

This function was introduced in Qt 4.1.
Which suggested to me that this didn't effect the socket binding. I may try giving it a whirl see what happens.

Like I say once the network architecture is fixed the issue may not be relevant as the routing tables would take care of it all. After posting yesterday I did manage to persuade my colleagues that the planned network architecture wouldn't work.

rickrvo
22nd December 2010, 15:22
I'm having the same problem too. I'm trying to make sure that the tcpsocket uses my wireless connection by setting the setLocalAddress() with my wireless ip and still it sends by the ethernet connection... If i disconnect the ehternet connection it sends on wireless...

I had to change the function setLocalAddress() in QAbstractSocket.h from protected to public otherwise using QTcpSocket wouldn't let me use the protected function, as it is not declared on QTcpSocket...

Have you found a solution to this problem?