QTcpSocket and specifying an outgoing network device client

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. Each is assigned an 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 assume this will work is data packets from the client starting at 192.168.1.3 (on some port), going to the router and then to the server at 192.168.1.2 (on some port) ... Okay, I hope this sounds reasonable.

Here's the problem. I cannot find a way to determine the outgoing address with QTcpSocket. There appears to be a binding method, but it doesn't do much. Every time I post this from a client, it moves to 192.168.1.2 by default.

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 protected
//socket->setLocalPort("50000"); // error, says it protected
//qDebug() << socket->localAddress();
//qDebug() << socket->localPort();

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

      

Any ideas?

+3


source to share





All Articles