Unsolved Receiving data from QTcpSocket
-
@SPlatten said in Receiving data from QTcpSocket:
right now it appears that the application is never getting past waitForConnected, even with a 20 second timeout.
Please don't be hurt, but I am not a friend of doing synchronous/blocking calls with Qt. Qt base design is asynchronous.
I prefer using signals/slots mechanism and I am usingconnected()
,disconnected()
,bytesWritten(qint64)
andreadyRead()
signals.My application needs to run on Android, Linux and Windows and
waitForConnected
has issues with Windows, from documentation:Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows.
-
@KroMignon , I'm happy to change, the problem is there are so many incomplete and what seem poor examples online.
Could the problem be because I am using both synchronous and asynchronous methods in my application?
In the class constructor:
connect(this, SIGNAL(connected()), this, SLOT(onConnected())); connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn())); connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
And then in the onConnect slot:
qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport; connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport); if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) { emit terminateModule(); }
-
@SPlatten said in Receiving data from QTcpSocket:
the problem is there are so many incomplete and what seem poor examples online.
https://doc.qt.io/qt-5/qtcpsocket.html#details - not enough examples there? And if not - write a good example, provide a patch for Qt and we will happily review it...
-
@Christian-Ehrlicher , when I have a working example I might just do that.
-
The Fortune Server/Client example is doing exactly what you try.
-
@Christian-Ehrlicher How do I download the Fortune Server and Client examples?
-
@SPlatten said in Receiving data from QTcpSocket:
How do I download the Fortune Server and Client examples?
Examples are part of a Qt installation (if using Qt installer).
-
@SPlatten said in Receiving data from QTcpSocket:
Fortune Server and Client examples
You can also just visit the doc pages, the links are at the end of the pages: https://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html & https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html
-
@SPlatten said in Receiving data from QTcpSocket:
And then in the onConnect slot:
qdbg() << clsModHelper::mscpszHost << muint16XMLMPAMport; connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport); if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) { emit terminateModule(); }
That is your problem!
You are "bad mixing" synchronous and asynchronous!OnConnected()
will be called when TCP server has accepted to the connection.
So you can start writing to server in this slot.
No need to usewaitForConnected()
==> you only have to use
connectToHost()
to start connection request to TCP server. -
@SPlatten said in Receiving data from QTcpSocket:
Could the problem be because I am using both synchronous and asynchronous methods in my application?
As @KroMignon says. Also I believe we have seen programs going wrong if they try to use the asynchronous signals at the same time as the synchronous
waitFor..
s, they get in each other's way IIRC.