Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Multiclient TCP server
I want to create a simple TCP server application that would handle multiple TCP clients. Normally I would create a separate thread for each connected client. In the past I have seen solutions concerning subclassing the
QThreadlike this one.
But I feel like this is an obsolete and an overkill approach (I do not sublass
QThreadfor a long time now). What would be the recommended modern Qt way of tackling this topic? Is using
QTcpClienteven the way to go, or is it unnecessarily to low level approach?
I would appreciate all help.
In the past I have seen solutions concerning subclassing the QThread like this one.
But I feel like this is an obsolete and an overkill approach
it is, mostly born out of a convoluted documentation
Take a look at the threaded fortune server example
should get you started
@J-Hilk Thank you for answer. I have seen this example already- it is subclassing the
you're right, I assumed it was updated 😡
Well, how you're actually supposed to do it, is now in the documentation,
See the detailed description of the QThread class
The first section
@J-Hilk Thank you for the link. In this case it is the way I am doing it for a while now. I thought maybe there are some new higher level approaches to this. Thank you for help!
The only thing you have to take care when subclassing QThread is that the (possible) slots in your object derived from QThread are executed in the correct thread by moving the derived object into the thread. Otherwise there is no real difference.
Here, have a spin. Opinions are also appreciated.
VRonin last edited by
Normally I would create a separate thread for each connected client
This is a myth. Follow @kshegunov 's link
@VRonin Thank you for answer.
I did mean a generic approach, in which the separate thread indeed is needed. Maybe in this design it will be unnecessary, but that's is also why I wanted to hear some opinions.
VRonin last edited by
1 client = 1 thread is actually unsustainable once the number of connected clients start increasing.
In the code linked to the chat example, both in the simple and advanced projects, you can easily see the clients are actually distributed among
QThread::idealThreadCount()threads rather than having 1 per client.
It also only makes sense if there is some actual work to do in that secondary thread
I see, I will check this topic out, sounds reasonable. Thank you.