Search code examples
qtqtcpserver

QTcpServer::hadPendingConnections() returns false upon new connection


I am working on some server application, which skeleton is subclassed QTcpServer, named UeTcpServer. The server app starts normally and when I place breakpoint in ueSlotTcpServerNewConnection(), which is connected to UeTcpServer's newConnectionSignal via

connect(this->ueTcpServer(),
        SIGNAL(newConnection()),
        this,
        SLOT(ueSlotTcpServerNewConnection()));

and then connect to server app using Linux terminal, breakpoint activates inside ueSlotTcpServerNewConnection() slot:

void UeMainWindow::ueSlotTcpServerNewConnection()
{
    if(this->ueTcpServer()->hasPendingConnections())
    {
        QTcpSocket* incomingSocket=this->ueTcpServer()->nextPendingConnection();

        this->ueSlotAddEventInfoLog(0,
                                    tr("New incoming connection from host")
                                    .append(" ")
                                    //.append(this->ueTcpServer()->nextPendingConnection()->peerAddress().toString())
                                    .append(incomingSocket->peerName())
                                    .append(" ")
                                    .append(tr("with IP address"))
                                    .append(" ")
                                    .append(incomingSocket->peerAddress().toString())
                                    .append(" ")
                                    .append(tr("using port"))
                                    .append(" ")
                                    //.append(this->ueTcpServer()->nextPendingConnection()->peerPort()));
                                    .append(QString::number(incomingSocket->peerPort())));
    }   // if
}   // ueSlotTcpServerNewConnection

However, this->ueTcpServer()->hasPendingConnection() returns false. Why?


Solution

  • I've forgot to call addPendingConnection(QTcpSocket *socket from incomingConnection(qintptr socketDescriptor) as is also stated in the the docs:

    void UeTcpServer::incomingConnection(qintptr socketDescriptor)
    {  
        QTcpSocket* newSocket=new QTcpSocket(this);
    
        newSocket->setSocketDescriptor(socketDescriptor);
    
        this->addPendingConnection(newSocket);
    }   // incomingConnection
    

    and it works now.