PDA

View Full Version : Implementing a TCP Server



keelerjr12
22nd August 2012, 15:57
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?

StrikeByte
22nd August 2012, 16:26
What is the run method? And what data needs to be send at what time interval?

some code would make it clearer.