Nominate our 2022 Qt Champions!

QTcpSocket: selecting source interface using bind() for connection to remote host with connectToHost(): don't work, possible bug

  • Hi, i want to use certain source interface for connection to remote host. I have
    3 interfaces: eth0 (, wlan0(, and lo:

    Assuming mSocket is class member:

    @QTcpSocket mSocket;@

    And in constructor we use:

    bool ret = mSocket.bind(QHostAddress(""));

    And then in timer we try to connect:

    void CamComponent::tryToConnect()
    mSocket.connectToHost(QHostAddress(CameraIpAddr), CameraPort);

    And after this i use TCPDUMP to view outgoing packets, and they dont have source IP Using strace i had checked that Qt makes calls to:

    socket() <- this causes problem, it's QT internal problem

    Is this any possibility to workaround this ? Maybe by using own sockets, creating descriptor and providing it to QTcpSocket by @QAbstractSocket::setSocketDescriptor@ ? Is this bug covers also QAbstractSocket::setSocketDescriptor ?

  • Hi.
    When you bind a socket using bind method then it randomly select a port number.Here i can see you didn't specify it.may be this cause an error because randomly selected port is not matched to your port.

    Is your CameraIpAddr same as ?

  • It is not server socket. Addresses are randomly chosen by me, not real. The clue is that bind() dont work for connect to.

  • Just to exclude any constructor overloading trap, have you tried:

    bool ret=mSocket.bind(QHostAddress(QLatin1String("")));


  • I think it is related to BUG:

    So my question, how to workaround this, or when bug will be fixed ?

Log in to reply