Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to wait on read and close event simultaneously on QTcpSocket
Forum Updated to NodeBB v4.3 + New Features

How to wait on read and close event simultaneously on QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 11.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aashish.lg
    wrote on last edited by
    #5

    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.

    Regards
    Ashish

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #6

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aashish.lg
        wrote on last edited by
        #7

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aashish.lg
          wrote on last edited by
          #8

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aashish.lg
            wrote on last edited by
            #9

            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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #10

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #11

                [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

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bu7ch3r
                  wrote on last edited by
                  #12

                  Just use a mutex an lock it before an unlock it when both request are finished.

                  for(int i = 200; i > 0;)
                  try
                  {
                  //do something
                  }
                  catch(...)
                  {
                  i--;//try again
                  }

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aashish.lg
                    wrote on last edited by
                    #13

                    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.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KA51O
                      wrote on last edited by
                      #14

                      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.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aashish.lg
                        wrote on last edited by
                        #15

                        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.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #16

                          Yes, of course there is. You should connect to the readyRead() signal, and only when you get this signal call readData().

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            aashish.lg
                            wrote on last edited by
                            #17

                            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.??

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved