Qt World Summit: Register Today!

Force QTcpSocket traffic over specific network interface

  • In the same PC, I have a wired and wireless network device (in Kubuntu 13.04), eth0 and wlan0, both with different IPs on the same subnet. I have a QTcpServer on each of them on different ports. The listen on wlan0 uses wlan0's IP (eg and the listen on eth0 uses eth0's IP (eg

    @QNetworkInterface wlan0 = QNetworkInterface::interfaceFromName("wlan0");
    tcpServer = new QTcpServer(this);
    tcpServer->listen(wlan0.addressEntries().first().ip(), 12346);
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(link()));@

    ..link() slot
    @tcp_client = tcpServer->nextPendingConnection();@

    From another linux machine on the same subnet, I make two client connections

    socat tcp: -
    socat tcp: -

    after which I do a tcp_client->write(a bunch of data); ...using the tcp_client associated to the wlan0 interface. The problem is that the data goes over eth0 instead of wlan0. I need to test that the wlan0 interface is working correctly. I suspect this isn't possible to control from the application and I may need to change my routing at the OS level. Can anyone confirm?

  • Qt Champions 2017

    You can't control this application level. It is the way routing, arp requests happen at TCP/IP level. You try disabling eth0 interface and work just with WLAN0 interface.

  • Thanks for the confirmation. I need to keep eth0 up for the tester command & control. I guess its off to the dreaded iptables.

Log in to reply