PDA

View Full Version : Tell QTcpSocket which interface to use



rianquinn
21st February 2007, 17:00
I have a Computer with three network interfaces. One for connecting to remotely (ssh), one for Internet, and another for my QTcpSocket connection. The third connection has to remain clean and only used by the QTcpSocket.

How do I make sure that when I open the socket, it uses the correct interface.

camel
21st February 2007, 17:14
I have a Computer with three network interfaces. One for connecting to remotely (ssh), one for Internet, and another for my QTcpSocket connection.


You are instanciating the connection? If you would be the server it would be rather easy, just tell the QTcpServer to listen (http://doc.trolltech.com/4.2/qtcpserver.html#listen) on the ip of the 3rd interface.



The third connection has to remain clean and only used by the QTcpSocket.

If you only connect to certain hosts using the qtcpsocket, the easiest way would be by routing only that traffic over the 3rd interface...



How do I make sure that when I open the socket, it uses the correct interface.
I do not think QTcpSocket has something builtin for that. What you could do, is create the tcpsocket using the methods of the operating system (which probably allow that kind of magic) and than


QTcpSocket *socket = new QTcpSocket;
socket->setSocketDescriptor(socketDescriptor);

rickrvo
22nd December 2010, 18:51
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?

squidge
22nd December 2010, 20:45
It will send via the default gateway installed on your PC. Change the default gateway to wireless and it'll quite happily ignore your ethernet connection, but I can guarantee yoiu that your ethernet has a higher priority, and thus is used by default.

Secondly, if you want to access protected members of a class, instead of modifying the Qt source code (which opens you up to legal hassle) why not subclass the class so the methods you want are public?



class mySocket: public QTcpSocket
{
public:
void setLocalAddress(const QHostAddress &address);
};

rickrvo
23rd December 2010, 10:53
I tried to create a mysock Class but an error occurred saying that there was no function by that name or something like that... :\

I know that it is selecting Ethernet by default... what I'm trying to do is at the start of my program I show all available ethernet and wireless connections and have the user to choose which one to use... because the program broadcasts and if a client is listening on ethernet the network is configured to ignore wireless broadcasts, and vice versa...

So I was wondering if is there a way to make QTcpSocket work with the desired interface.

squidge
23rd December 2010, 14:19
I've tried the code I pasted above, and it compiles without errors.

I don't think selecting a network interface is possible in a cross-platform way.

rickrvo
23rd December 2010, 14:47
I tried to change and use the mysock Class again as you said.. and the result was "c:\Qt\4.7.1\include/QtNetwork/../../src/network/socket/qabstractsocket.h:205: error: 'void QAbstractSocket::setLocalAddress(const QHostAddress&)' is protected" :\

Setting the interface to use may not be possible in a cross-platform way but what if I only use windows?

How about changing the networks priority? is it possible by code?

squidge
23rd December 2010, 18:28
Please post code so we can see exactly what you are doing.

As for the network, you can use Windows Management Intrumentation (WMI) to change the default gateway or change the metric of the interfaces.