QTcpServer :: hadPendingConnections()在新连接时返回false

时间:2016-07-08 08:42:04

标签: qt qtcpserver

我正在处理一些服务器应用程序,该骨架是子类QTcpServer,名为UeTcpServer。服务器应用程序正常启动,当我在ueSlotTcpServerNewConnection()中放置断点时,该断点连接到UeTcpServer' s newConnectionSignal来自

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

然后使用Linux terminal连接到服务器应用,断点在ueSlotTcpServerNewConnection()插槽内激活:

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

但是,this->ueTcpServer()->hasPendingConnection()会返回false。为什么呢?

1 个答案:

答案 0 :(得分:0)

我忘了从addPendingConnection(QTcpSocket *socket致电incomingConnection(qintptr socketDescriptor),正如docs中所述:

void UeTcpServer::incomingConnection(qintptr socketDescriptor)
{  
    QTcpSocket* newSocket=new QTcpSocket(this);

    newSocket->setSocketDescriptor(socketDescriptor);

    this->addPendingConnection(newSocket);
}   // incomingConnection

现在有效。