How to wait on read and close event simultaneously on QTcpSocket
-
Hi,
I would like to disscuss about the design approach suggested by you earlier , mimic the func of sockets in a separate thread for faster comm.
Here is the design what I thought:
I am creating the socket in a class connecting it to server, from that class I am launching separate therad of execution.
Inside run method of that thread , I will merely connect the signals(readyRead, Disconnect, error)
and call exec(). All signals will be associated with the slots defined in the thread class only.
Based on some data arrival from server in these slots I will be calling thread owner class to do some stuff on QWebView.
Please suggest is this a proper approach to go with async socket comm.
-
In addition to add in the above discussion I am getting below mention messages in DbgViewer.
[2684] QSocketNotifier: socket notifiers cannot be disabled from another thread
[4040] QSocketNotifier: Multiple socket notifiers for same socket 960 and type Read
-
Your issue is probably, that the sockets are not created in the socket thread, but in the main thread. And be very careful with triggering UI changes from a thread. You can use queued signal-slot connections only for that.
-
[quote author="Ashish Mittal" date="1326435620"]In addition to add in the above discussion I am getting below mention messages in DbgViewer.
[2684] QSocketNotifier: socket notifiers cannot be disabled from another thread
[4040] QSocketNotifier: Multiple socket notifiers for same socket 960 and type Read
[/quote]I can only interpret the error messages you see.
Presumably you are using a Socket several times and assign it to different threads. This may happen when using:
@
QTcpSocket sckt;for ( int i = 0; i < n; ++i )
{
assignSocket ( &sckt );
}
@Better would be:
@
for ( int i = 0; i < n; ++i )
{
QTcpSocket *sckt = new QTcpSocket;assignSocket ( sckt );
}
@
When assignSocket is the method you are using to assign sockets to different threads you are using the same socket for all threads. In the second case you will have independent sockets.Note: just example code not tested
-
Just use a mutex an lock it before an unlock it when both request are finished.
-
Hi,
I am not getting "QSocketNotifier:bla bla..." messages if I use Async approach.
I am totally confused at this movement , what should be the correct desing for this kind of application in Qt.Please suggest :-
Can I derive a class from QThread, and inside that class
I will create a socket and QWebView, and then I will connect slots for network comm inside run method and then based on slots call , I will manipulate the webview.Please suggest the appropriate design.
-
You can only manipulate GUI elements inside the main thread. What you could do is get the content from the WebView (e.g. via getHtmlText() or something) pass the resulting string to your worker object which resides in another thread manipulate the string there and pass it back to the main thread and then finally update your WebView.
also read "this article":http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/, you should not subclass QThread. You can do it but its not the intended use anymore.
-
Hi,
Is there no way in Qt to get the data from the QTCPSocket without calling waitForReadyRead() function.
I tried to call the function readData() , but it always returns 0, if I am calling waitForReadyRead() then calls readData() , then it succeeds.
Actually prob is if I am in waitForReadyRead() then it socket closure happpens at server then this is giving me crash.
One more question is , I have created my sockets in worker thread , and register readyRead signal with some slot in that thread only.
I have two diff socket at client created in two diff thread.
When I print threadID in data ready read slot, I am getting same thread id, I am confused it should be different thread id.Please correct my understanding.
-
Yes, of course there is. You should connect to the readyRead() signal, and only when you get this signal call readData().
-
Thanks for your qucik reply. As of now I have coded the way suggested by you.
But my confusion is , if readyRead signal is calling the slot then which thread will call this slot
Is this a worker thread or the GUI thread(main app thread) ?My main app is creating multiple thread and I am creating one socket in each thread and registering readyRead() signal in each thread with some slot in that thread class only.
If I am printing thread id in the slot it is giving same as GUI thread id.
It means only GUI thread is calling this slots, ideally it should be worker thread, otherwise what is the point of using worker thread.One more doubt is this SLOT is unique call back for the entire app, ot will be different for each thread class.??