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. Implementing a TCP Server
Qt 6.11 is out! See what's new in the release blog

Implementing a TCP Server

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 3.3k 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.
  • K Offline
    K Offline
    keelerjr12
    wrote on last edited by
    #1

    Can anyone tell me if I'm doing this right?

    Using Qt I'm implementing a TCP Server by inheriting from the QTcpServer class. On an incoming connection I create a new thread, a new Worker object, and I move the object to the new thread and start the thread. From here, the server keeps listening for new clients and each thread then is in its run method for object Worker.

    Now, I create a timer because I need to send updates to each client based on 1 second intervals AND when a song is playing. In the readyRead slot I read data using readAll and then perform some work and send a reply.

    However, when I go back to my run method I need to just continue sending song data updates to the clients (with no response from the client). Should this all just go in a while(true) loop and then I check some boolean to start and stop the timer? The track information I need to send is the song progression time.

    I guess my question is, should I be doing it this way? It seems a little complex, but then again that's concurrency for you. Basically I need the TCP server to send data to the client repeatedly when some condition is true. I feel like just an endless while loop that checks when to start and stop the timer is useless work.

    Would posting code make this clearer?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Isn't it an idea to implement the timer in the worker object. Then every thread is running on its own. You could also use the QTime::elapsed () function to check if a second has passed in the worker thread. Maybe something like this:
      @
      while (keepOnRunningSong)
      {
      if ((MyTimer.elapsed() % 1000) == 0)
      {
      SendTheSongTime();
      }
      // Send song data here
      }
      @
      Maybe post some code how the server handles new connections and the worker class as well.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • I Offline
        I Offline
        IanBorn
        wrote on last edited by
        #3

        bq.
        Now, I create a timer because I need to send updates to each client based on 1 second intervals AND when a song is playing. In the readyRead slot I read data using readAll and then perform some work and send a reply.

        Since you already have a readyRead signal, why don't you just keep sending that chuck of stream after each readAll? Let the client taking care of the the queue?

        BTW, I am familiar only with audioinput, do you use Phonon to play the song? And how you get signal readyRead and readAll?
        I have a neat way to get more clients to the server by employing Network tree.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          [quote author="Jeroentje@home" date="1345712541"]You could also use the QTime::elapsed () function to check if a second has passed in the worker thread. Maybe something like this:
          @
          while (keepOnRunningSong)
          {
          if ((MyTimer.elapsed() % 1000) == 0)
          {
          SendTheSongTime();
          }
          // Send song data here
          }
          @ [/quote]

          This will give you 100% CPU for that thread, just because of polling MyTimer.elapsed() as fast as it can. Polling is bad style, using timer signals is way better.

          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