Results 1 to 4 of 4

Thread: Binding a QTcpSocket to a specific enternet interface

  1. #1
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Binding a QTcpSocket to a specific enternet interface

    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

    Cheers
    Banfa

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Binding a QTcpSocket to a specific enternet interface

    Have you tried using QAbstractSocket::setLocalAddress()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Banfa (2nd June 2009)

  4. #3
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Binding a QTcpSocket to a specific enternet interface

    Hmmmm, I had considered that function but the help says

    Quote Originally Posted by QT Creator Help
    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.
    Last edited by Banfa; 2nd June 2009 at 10:27.

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

    Default Re: Binding a QTcpSocket to a specific enternet interface

    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?

Similar Threads

  1. Tell QTcpSocket which interface to use
    By rianquinn in forum Qt Programming
    Replies: 7
    Last Post: 23rd December 2010, 17:28
  2. Replies: 4
    Last Post: 2nd September 2007, 19:48

Tags for this Thread

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.