Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.14.2
-
None
Description
From Qt5, our softwares face some difficulties connecting to a remote host with QTcpSocket.
Our code was initially developped with Qt4.74 and we recently upgraded to Qt5.9.1.
On a computer equipped with multiple network interfaces, sometimes I can't connect to a remote host.
Network configuration:
- WiFi network: 192.168.1.18 / 255.255.255.0
- Ethernet: 192.169.5.151 / 255.255.255.0
- Destination host: 192.169.5.154 / 255.255.255.0
I have a small QTcpServer which listen to port 3000 on the destination host.
Here is a code snippet that works fine with Qt4 and not anymore with Qt5 (I tested with Qt 5.9.9 and Qt5.14.2 as well)
QTcpSocket *pSocket = new QTcpSocket(); connect(pSocket, SIGNAL(connected()), this, SLOT(onConnected())); pSocket->connectToHost("192.169.5.154", 3000);
The signal "connected" is emitted even though I can't see any connected socket on server side.
If I set the IP of the remote host to "192.168.5.154" and change the code accordingly, it works.
If I bind to the local network interface before connecting, it works:
pSocket->bind(QHostAddress("192.169.5.151"));
If I create the socket manually, it works:
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); pSocket->setSocketDescriptor(sock, QAbstractSocket::UnconnectedState);
Is there a way to have it working natively with Qt without any workaround?