Reputation: 123
i want to use certain source interface for connection to remote host. I have 3 interfaces: eth0 (192.168.230.100), wlan0(10.0.9.100), and lo:
Assuming mSocket is class member:
QTcpSocket mSocket;
And in constructor we use:
bool ret = mSocket.bind(QHostAddress("10.0.9.100"));
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 10.0.9.100. Using strace i had checked that Qt makes calls to:
What is workaround for this problem ?
Upvotes: 0
Views: 1563
Reputation: 2969
This issue is not Qt related. You just have to understand a bit how a TCP/IP stack work.
When you try to connect to a remote server, your provide its IP address. Internally, your operating system will choose an appropriate out interface to send these connection packets, according to its routing table. (I will not speak to much about conflicts and default routing here, Google should be enough to make your culture)
So, you just have to use your QTcpSocket as client socket, connectToHost
and let the resolution magic happened. The socket binding is only meaningful in a server application.
Upvotes: 1