0

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?

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
  • 1
    Having looked at my own usage of `QTcpServer` I tend to use `while (QTcpSocket *conn = server->nextPendingConnection()) {` and have never had any problems. So my question is... when `hasPendingConnections` returns false does `nextPendingConnection` return NULL/nullptr -- or does it actually return a valid connection? – G.M. Jul 08 '16 at 09:22
  • @G.M. if I remove/comment `if(this->ueTcpServer()->hasPendingConnections())`, I get `NULL` pointer to `QTcpSocket`. – KernelPanic Jul 08 '16 at 09:27
  • 1
    Did you reimplement incomingConnection in your subclass of QTcpServer in your application. If so, maybe you forget to call addPendingConnection. – Kirill Chernikov Jul 08 '16 at 09:41
  • That was it! I've forgot to call `addPendingConnection(QTcpSocket* socket)` from `incomingConnection(qintptr socketDescriptor)`. Now it works! – KernelPanic Jul 08 '16 at 09:52

1 Answers1

0

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.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90
  • 2
    you don't need to override `incomingConnection` if this all what you want to do, as this is exactly what the [base implementation](https://code.qt.io/cgit/qt/qtbase.git/tree/src/network/socket/qtcpserver.cpp#n579) does. – Mike Jul 08 '16 at 10:24
  • 1
    Why `Q_UNUSED` if you use the function parameter...? – peppe Jul 08 '16 at 20:38