Implementing a TCP Server



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



  • 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



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



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.