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 use QTcpSocket with Threads
Forum Updated to NodeBB v4.3 + New Features

How to use QTcpSocket with Threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 776 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.
  • G Offline
    G Offline
    GCE_
    wrote on 23 Sept 2020, 09:03 last edited by
    #1

    Hi guys!
    In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
    All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
    I want to use the Sockets with Threads and I have no prior experience with threads in QT.
    I hope you guys can help me.

    Best regards

    J G 2 Replies Last reply 23 Sept 2020, 09:09
    0
    • G GCE_
      23 Sept 2020, 09:03

      Hi guys!
      In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
      All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
      I want to use the Sockets with Threads and I have no prior experience with threads in QT.
      I hope you guys can help me.

      Best regards

      J Offline
      J Offline
      JonB
      wrote on 23 Sept 2020, 09:09 last edited by JonB
      #2

      @GCE_ said in How to use QTcpSocket with Threads:

      I want to use the Sockets with Threads and I have no prior experience with threads in QT.

      Why? As you say, threads are indeed difficult, especially for a beginner!

      Qt sockets are asynchronous by nature, meaning they don't block and you can have multiple sockets opened/serviced without one blocking/interfering with another.

      As you say, if you use the waitFor...() calls you will block on that socket, and would indeed need threads to prevent that. Don't use those calls at all! Look at the signals/slots in QTcpSocket/QAbstractSocket and use only those, then you won't need any blocking calls or threads :)

      1 Reply Last reply
      4
      • G GCE_
        23 Sept 2020, 09:03

        Hi guys!
        In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
        All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
        I want to use the Sockets with Threads and I have no prior experience with threads in QT.
        I hope you guys can help me.

        Best regards

        G Offline
        G Offline
        Gojir4
        wrote on 23 Sept 2020, 11:19 last edited by
        #3

        @GCE_ Hi, you don't need threads for that, instead of waitForReadyRead()you can use readyRead() signal and read from socket at this time. This will avoid blocking your event loop and freeze your GUI.
        see Fortune Client Example

        1 Reply Last reply
        4

        1/3

        23 Sept 2020, 09:03

        • Login

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