How to wait on read and close event simultaneously on QTcpSocket
I wrote one thread based application, where inside the run() of thread I am waiting for readyread using WaitForReadyRead(-1), infinitely. But it crashes here when socket has been closed at the server.
How can we wait for read and close event at once so that I can return from run() function safely in case of socket closure.
You could wait for a definite time interval in a while loop with the condition that the connection is still established through check of state [developer.qt.nokia.com] .
I tried to wait for 1 second and then whenever be timeout occur I am checking the socket state, but neverthless it crashes in waitforreadyread again.
I am looking for something similar to WSAEventSelect function whereas we can put socket into FD_READ and FD_CLOSE events and can use waitforobjects for these event handles.
IMHO it is not a good design to open the socket and just wait until something will be sent. Typically you establish a connection and connect the readyRead signal [developer.qt.nokia.com] signal with a slot handling the reading of the information. You have then the possibility to check the number of bytes received.
When the connection to the dies, then the socket is dead and you might want to remove it or reestablish the connection.
Did you have a look to the fortune example code [developer.qt.nokia.com]
Yes I have gone through that code(blocking and nonblocking fortune) and implemented that signal and slotting mechanism as well. But it brings slowness in my application when sockets are more(e.g. 5), and we are getting data from the server on all these sockets.
Thats the reason I created one thread per socket to server the requests, but got strucked at this point.
Anyways thanks a lot for your suggestions….
These discussion are really making my life easily.
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.
 QSocketNotifier: socket notifiers cannot be disabled from another thread
 QSocketNotifier: Multiple socket notifiers for same socket 960 and type Read
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
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 [labs.qt.nokia.com], you should not subclass QThread. You can do it but its not the intended use anymore.
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.