connectToHost and waitForConnected
-
Confused, I'm calling connectToHost:
connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport); if ( waitForConnected(clsModHelepr::mscintConnectionTimeout) != true ) { exit terminateModule(); }
mscpszHost contains "localhost"
muint16XMLMPAMport contains 8123
mscintConnectionTimeout contains 20000;The timeout is in milliseconds, so 20 seconds. I'm absolutely sure nothing else is using 8123 on my system, but when using the debugger I can see that the waitForConnected is timing out instantly and not after 20 seconds at all, why ?
[Edit] Modified code to:
QElapsedTimer tmr; tmr.start(); connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport); if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) { cout << tmr.elapsed() << endl; emit terminateModule(); } `` In the Application Output, 4 is displayed.
-
Did you actually check which error you get? I mean - the OS will not wait 20 seconds until it tells you that e.g. the connection was refused.
-
Are you on MS Windows?
Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows.
(from: https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected)
What timeout do you get, when you use the recommended alternative, the
connected()
- signal?https://doc.qt.io/qt-5/qabstractsocket.html#connected
The
connected()
signal documentation also states, that it may stop instantly, when connecting tolocalhost
. -
@Pl45m4 No, iMAC:
iMac (Retina 5K, 27-inch, Late 2015) Processor 4 GHz Quad-Core Intel Core i7 Memory 16 GB 1867 MHz DDR3 Graphics AMD Radeon R9 M395X 4 GB
Qt:
Qt Creator 4.13.2 Based on Qt.5.15.1 (Clang 11.0 (Apple), 64 bit) Built on Oct 1 2020 01:16:45 From revision 2ee1af2032
My application is using Qt Kit:
Desktop Qt 5.15.0 clang 64bit
Before the code I do have connections to signals:
connect(this, SIGNAL(connected()), this, SLOT(onConnected())); connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn())); connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
My class is derived from QTcpSocket.
-
Did you actually check which error you get? I mean - the OS will not wait 20 seconds until it tells you that e.g. the connection was refused.
-
@Christian-Ehrlicher , thank you, the onErrorOccurred does get error 0. 0 = Connection Refused. Which I guess is correct if no server is up using that port.
-
It's not about the port, but about the computer. As far as I understand the docs, it will accept / terminate the connection instantly when you connect to your local machine, which you do. So you probably never gonna get close to 20 secs.
@SPlatten said in connectToHost and waitForConnected:
0 = Connection Refused. Which I guess is correct if no server is up using that port.
Open ports shouldn't refuse connection requests. Any firewall that could block it?
Found out, that 8000-8999 are used by different Apple products (iTunes, irdmi service)
https://support.apple.com/en-us/HT202944 -
-
@SPlatten
FTR: A TCP/IP connection is a unique combination of four numbers: server IP + port + client IP + port. the same combination cannot be reused. Usually the client specifies the server port to connect on but not which port to use from client side. That gets filled in during the connection (e.g.connectToHost()
) with a "random", unused port number at client side. That's how the same client can make multiple, separate connections to the same server service port. You can see this by looking at the port numbers used at client side during, say, multiple FTP connections.