Application does not quit correctly after a call to QTcpServer::listen fails



  • Hello,
    I am using Qt 4.8.4 on Windows 8 and I have the following problem: when for some reason a call to QTcpServer::listen fails (for example because the address or the port number are not available) my application should display an error and quit.
    Instead of quitting completely though, the process keeps living for more or less three minutes (visible in the task manager) and can not be killed. After some time (as I said, more or less three minutes, but I didn´t measure exactly) it disappears by itself. During this time the port can not be used by other instances of the application of course.
    If the call to QTcpServer::listen was successful, this does not occur, I can close the server and terminate the application without problems. The port is available again immediately afterwards.
    I imagine this is somehow related to socket timeouts at system level, but it would be good to get a clear explanation and a solution for this, if possible.
    I attach a code example. With this example the problem can be reproduced on my machine. Note that in my example DataHandler is a dummy QObject which does nothing, so its code is irrelevant.

    Can anybody help?
    Thanks in advance!

    @
    #include <QtCore/QCoreApplication>
    #include <QTcpServer>

    #include "DataHandler.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QTcpServer* server = new QTcpServer();

    bool ok = server->listen(QHostAddress::Any, 80);

    if (!ok)
    {
    server->close();
    delete server;
    return -1; //In this case the application does not quit correctly
    }

    DataHandler* dataHandler = new DataHandler();

    QObject::connect(server, SIGNAL(newConnection()), dataHandler, SLOT(sendData()));

    a.exec();

    delete server;
    delete dataHandler;

    return 0; //In this case the application quits correctly
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.