Move QTcpSocket to a new thread after the start of the connection

I have a multithreaded server.

QTcpSocket must be created in the stream in which it should be run, FI: Qt - process QTcpSocket in a new stream , passing the socket descriptor.

My problem is that I need to have a pool of threads and move the socket along a specific thread AFTER the client has sent a specific token that determines which thread the socket should be in.

In other words, I need to read the socket to find out on which stream it will be placed in advance.

Some idea would be to first bind a QTcpSocket, read, then send the handle to the stream and create another QTcpSocket, but the document says:

Note. It is not possible to initialize two abstract sockets using the same native socket descriptor.

Another solution is to create a socket in a separate thread and then combine both threads together, although I don't know if this is possible.

Or maybe you can read the socket descriptor in the main thread before calling setSocketDescriptor in the child stream, if possible?

+5
source share
1 answer

You can move sockets through QThreads absolutely easily, just pay attention to four things:

1) Make sure your QTcpSocket does not have a parent before moving

2) Disconnect all socket objects before moving

3) Connect everything you need to the function that is running in the destination stream (you can use some pool in the stream where those "moved" objects are stored before the stream picks them up

4) Call readAll () after init, as you can skip some readyRead () signals

I see no reason not to do this if it is suitable for the design, at least I used it many times for multi-threaded services to separate the socket handlers by core.

0
source

All Articles