QTcpSocket and Specifying Client Outgoing Network Device



  • Windows 8.1 user here, using Qt 5.3. Trying to learn network programming (please bear with me). Let's say I have two network devices on my machine. One is assigned the IP 192.168.1.2, and the other 192.168.1.3. The first device has priority.

    My goal is to create a QTcpServer on 192.168.1.2 and a QTcpSocket client on 192.168.1.3. The way I envision this would work is the data packets from the client will start at 192.168.1.3 (on some port), travel to the router, then to the server at 192.168.1.2 (on some port). Hopefully this sounds reasonable.

    Here's the problem. I can't find a functioning way to specify an outgoing address/device with QTcpSocket. There appears to be a bind method, but it doesn't do much. Each time I send that from the client, it travels on the default device at 192.168.1.2.

    Any ideas?

    socket = new QTcpSocket(this);
    qDebug() << socket->localAddress(); // shows "0"
    qDebug() << socket->localPort(); // shows "0"

    socket->bind(QHostAddress("192.168.1.3"), 50000);
    qDebug() << socket->localAddress(); // shows "50000"
    qDebug() << socket->localPort(); // shows "0"

    //socket->setLocalAddress(QHostAddress("192.168.1.4")); // error, says it's protected
    //socket->setLocalPort("50000"); // error, says it's protected
    //qDebug() << socket->localAddress();
    //qDebug() << socket->localPort();

    socket->connectToHost("google.com", 80); // network manager shows data on 192.168.1.2



  • Can anyone lend a hand? Thanks!



  • HI and welcome to devnet,

    you can access to the native socket descriptor with http://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor and use native API to do this (is possible to do in Linux but I don't know about WIndows or OS X)



  • @radian can you check whether the bind() method returns true? And if it doesn't, what does error() say? Most probably something is going wrong that you did not think of and the first thing to do is to check error reporting.



  • @mcosta Thanks, I'll look into it. Are there any resources out there on manipulation of the socketDescriptor?

    @cybercatalyst It actually returns True, so must be working?

    So I tried another test, and am finding myself even more confused. I created a TCP server on 192.168.1.3 (a physical USB to Ethernet adapter device, called "Ethernet2") and then used telnet to open a socket to 192.168.1.3 from 192.168.1.2 (computer main network adapter, called "Ethernet").

    Sending and receiving data between the server from telnet works fine. The strange thing is that Windows network monitor for Ethernet2 shows no change in data transmitted or received.

    I would have thought that data should be changing equivalently between Ethernet and Ethernet2, but that doesn't appear to be the case. Just to be clear, both the computer ethernet device and USB ethernet device are connected to the router.

    Any idea why this would be the case?



  • So you assume the measurements are wrong? Can you try with a better tool, like Wireshark? This shows you single packets and all details.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.