Important: Please read the Qt Code of Conduct -

How to wait on read and close event simultaneously on QTcpSocket

  • Hi,

    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": .

  • 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": 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 ":

  • 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 assume that you just have to put the whole signal-slot mimick for one socket into one thread. That is probably the easiest path to move forward.

  • Ok!!! Sounds a nice approach, let me give a try to this and then I will come back.

  • 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

    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":, 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.??

Log in to reply