Solved QSslSocket::readyRead() signal is not raise after sending data ..
-
Hi,
Out of curiosity, why allocate block on the heap ? Since you delete it anyway at the end of the function, just allocate it on the stack. You'll avoid all the troubles you had with deleting it by hand before it was completely sent.
-
@Ritchie:
I just saw from your last code that you also havem_sslSocket->waitForEncrypted(-1);
. In principle, I would avoid mixing thewaitForXxx()
functions with the signal-slot mechanism.I also just recognized, that your code runs within a thread, correct? Using blocking functions is totally valid in threads, but as said above, I'd not mix this with signals-slots.
Further, a thread does not use the event loop of your main program. If you want to use slots in your thread, than you need to implement an event loop for it. Emitting signals from within you thread (e.g. to the main thread) is always possible, but using slots within your thread needs an separate event loop.
And maybe this is already the problem why your slot is only called when you have the
waitForReadyRead()
function active? -
@aha_1980
But when I remove the linem_sslSocket->waitForEncrypted(-1);
the complete communication will not established.
This was posted here:
https://forum.qt.io/topic/84370/create-a-ssl-connection-only-with-slots-and-signalsThere is also a sample program posted (download because of to big files).
I do not get the "encrypted()" signals in this case and I do not know, why.
But with the "m_sslSocket->waitForEncrypted(-1);" I got the signal.Best regards
R. -
Hi,
I just reinstall the complete system to make sure that it is not my installation.
But the problem is still existing.- The server program receives a request for a new connection and start a new instance of the SSLSocket.
But after the command
m_sslSocket->startServerEncryption();
on the server nothing happens on the client. Still!
But now I at a slot
connect(m_sslSocket,SIGNAL(connected(void)), this, SLOT(ConnectionEstablished(void)));
and noticed, that when I close my frontend application which is working
like the sample application of QML, here the start code of the windowQCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); app.setOrganizationName(CLIENT_PROGRAM_NAME); QTextCodec::setCodecForLocale( QTextCodec::codecForName("UTF-8") ); QLocale::setDefault(QLocale::German); QQuickStyle::setStyle(QStringLiteral("qrc:/qml/Style")); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("BackEnd", &UserDisplay); engine.load(QUrl(QStringLiteral("qrc:/client.qml")));
i receive a "Connected" signal.
That sounds for my, that my QML Application is blocking signals, which I do not know.
Is it allow to have a front end, like the sample program and running a back ground program for communication ?
Best regards
R. -
@Ritchie we already gave you some pointers.
Again: don't mix
waitForReadyXxxx()
with signals&slots!If your complete program is not working, its often best to start from scratch with a minimal working example. then build your way to a full program. when you stuck, ask specific questions, we may then be able to help you. most often you can answer you question yourself already.
-
Hi,
Again: don't mix waitForReadyXxxx() with signals&slots!
I do not use any "waitForReadyXxxx()" within my program.
I used once "m_sslSocket->waitForReadyRead(10);" to show, that suddenly a change was happen. But it solve not my problem.Anyways I guess the main reason why it does not work it,
that I have to call "QThread:exec()" with the thread once to support signal and slots in the QThread.But when I call "QThread:exec()" I will not execute any other code in the class.
But I would like to execute the loop which is coded in the function "void DataThread::doWork(void)"
How can I execute "void DataThread::doWork(void)" and let "Qthread:exec()" have at the same time ?
Best regards
R. -
You should take a look at the example provided in the QThread details documentation more specifically the one for the worker object implementation.
-
Hi,
I am already using the first way, shown in the examples.
But the documents says:
A QThread object manages one thread of control within the program. QThreads begin executing in run(). By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.
So, if I not execute the "Exec()" command, so far I understand, the QT Event Loop will not starts.
But within the examples, there is no using any "exec()".
It says for the first sample:
The code inside the Worker's slot would then execute in a separate thread. However, you are free to connect the Worker's slots to any signal, from any object, in any thread. It is safe to connect signals and slots across different threads, thanks to a mechanism called queued connections.
So in this case, no "exec()" is needed in the separate thread to execute ?
And for the second sample it says
In that example, the thread will exit after the run function has returned. There will not be any event loop running in the thread unless you call exec().
Best regards
R. -
@Ritchie I'm assuming your "program" is a client connecting securely to a server, because you're using
m_sslSocket->write(*block); m_sslSocket->flush();
so I don't getting why you're expecting a readyRead() signal, since that will happen when you are reading data, not writing it. From Qt's documentation:
void QIODevice::readyRead()
This signal is emitted once every time new data is available for reading from the device's current read channel.
Am I missing something? If so my apologies right now...
-
@Pablo-J-Rogina
You are right, thevoid QIODevice::readyRead()
is only emitted after receiving bytes from the server.
And after sending data's from the client to the server, the server will answer.Like "Data stored" correct or "Data not stored"
Best regards
R. -
Hi to all,
I solved my problem by the following way.
The function "DoWork" was change in the follwing way. Now it calls the QThread::exec() function.
The loop function is done by a timer event.void DataThread::doWork(void) { ProgramInfo->writelog(QObject::tr("carclient"),QObject::tr("DataThread"),QObject::tr("Info"),QObject::tr("Thread startup")); if (! m_sslSocket->supportsSsl()) { ProgramInfo->writelog(QObject::tr("carclient"),QObject::tr("DataThread"),QObject::tr("Failure"),QObject::tr("No support fpr SSL connection!")); } m_WorkTimer = new QTimer(); connect(m_WorkTimer, SIGNAL(timeout()), this, SLOT(DoTimerWork())); m_WorkTimer->start(50); QThread::exec(); m_WorkTimer->stop(); // will delete later if( m_StateMachineState != NoConnection ) { m_sslSocket->close(); m_sslSocket->disconnectFromHost(); } emit WorkFinished(); }
Within the function "DoTimerWork", I do the cyclic stuff and do not block the events anymore
void DataThread::DoTimerWork(void) { if( SensorQueryBuffer->Size() > 0 ) { if ( ! SensorQueryBuffer->locked() ) { if (m_sslSocket->state() != QTcpSocket::ConnectedState) { if( m_StateMachineState == NoConnection ) { m_StateMachineState=TryToConnect; m_sslSocket->connectToHostEncrypted(m_ServerTCPIPAddress, m_ServerTCPIPPort); } } } } }
Thanks to all try to help me.
Best regards
R.