0

I have a simple multithreading application that works like the example "Threaded Fortune Server Example" on Qt website.

When my QTCPServer receive an incoming connection, I create a QRunnable task, passing the socketDescritptor and then submitting it to a QThreadPool:

void Server::incomingConnection(qintptr socketDescriptor)
{
    Task *task = new Task(socketDescriptor, this);
    this->m_pool->start(task);
}

then in the Task method run() I create the socket:

void Task::run()
{
    QTcpSocket tcpSocket;
    if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
        emit error(tcpSocket.error());
        return;
    } 
...
}

However, I read those note about incomingConnection method of QTCPServer from the Qt Doc

Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection().

Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.

So, my questions are:

  • how can I add the socket created from another thread to the pending connection of the Server?
  • is this a needful operation?

Thanks,

BEPP1
  • 945
  • 2
  • 11
  • 15
  • Your code should be fine, you are not relying on the `nextPendingConnection()` mechanism or the `newConnection()` signal. – Kevin Krammer Jan 27 '17 at 10:10
  • Thanks for the answer. Sometimes, my application crash with "QThreadPipe: Unable to create pipe: Too many open files", expecially when the number of the incoming connection increasing. I was wondering why this happens. – BEPP1 Jan 27 '17 at 10:24

0 Answers0