Results 1 to 8 of 8

Thread: Tell QTcpSocket which interface to use

  1. #1
    Join Date
    Jan 2006
    Posts
    30
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Tell QTcpSocket which interface to use

    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.

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tell QTcpSocket which interface to use

    Quote Originally Posted by rianquinn View Post
    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 on the ip of the 3rd interface.

    Quote Originally Posted by rianquinn View Post
    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...

    Quote Originally Posted by rianquinn View Post
    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
    Qt Code:
    1. QTcpSocket *socket = new QTcpSocket;
    2. socket->setSocketDescriptor(socketDescriptor);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2010
    Posts
    71
    Thanks
    26
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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?

    Qt Code:
    1. class mySocket: public QTcpSocket
    2. {
    3. public:
    4. void setLocalAddress(const QHostAddress &address);
    5. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by squidge; 22nd December 2010 at 19:58.

  5. #5
    Join Date
    Dec 2010
    Posts
    71
    Thanks
    26
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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.

  7. #7
    Join Date
    Dec 2010
    Posts
    71
    Thanks
    26
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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?
    Last edited by rickrvo; 23rd December 2010 at 14:35.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tell QTcpSocket which interface to use

    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.

  9. The following user says thank you to squidge for this useful post:

    rickrvo (12th January 2011)

Similar Threads

  1. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 17:56
  2. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25
  3. User Interface with QTableView
    By Brandybuck in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2006, 23:24
  4. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50
  5. Qt interface running extremely slowly...
    By jazztpt in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 11:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.