Important: Please read the Qt Code of Conduct -

How to set QTcpServer to listen exclusivly using Win7

  • I use a QTcpServer that should listen alone on port. Language is c++ with Qt 5.9. The application must run under Win and Linux using MinGW. The listen method from QTcpServer uses standard parameter for socket options. For Win10, Linux these options are set default to single usage of the listening port so listening works fine. Unfortunatly opposite to that Win7 offers shared usage which i must avoid. I figured out that the QAbstractSocket class let me create a socket with the BindFlag::DontShareAddress. I can forward the socketdescriptor to the QTcpServer. Then the method listen fails (isn't listening) by stating: QTcpServer::listen() called when already listening. I check the ports status by using netstat. My code sample is below:

    bool TcpServer::init(QString ipAddress, quint16 port, Command::RoutingProperty clientSocketKind, QString interfaceName)
        if (mServerIsInit == true) // only 1 server instance
            return false;   
        mServer = new (std::nothrow) QTcpServer();
        if (mServer == nullptr)
            return false;
        mClientSocketKind = clientSocketKind;
        mInterfaceName = interfaceName;
    // tries to set socket properties to a non sharing port
        QTcpSocket tsocket;
       if (!tsocket.bind(QHostAddress(ipAddress), port, QAbstractSocket::BindFlag::DontShareAddress))
            qDebug() << "Socket bind fails";
            qDebug() << "Socket bind success";
        sd = tsocket.socketDescriptor(); // valid socket descriptor
        if (!mServer->setSocketDescriptor(sd))
            qDebug() << "SocketDescriptor fails";
        sd = mServer->socketDescriptor();
        qDebug() << "Socketdescriptor Server " << sd;
    //	bool setOk = tsocket.setSocketDescriptor(sd, QAbstractSocket::SocketState::ListeningState, QIODevice::ReadWrite); // fails 
    //end tries to set socket properties to a non sharing port
        if (mServer->listen(QHostAddress(ipAddress), port)) // fails with message ... is always listening
    //	if (mServer->mServer->isListening()) // is not listening tells netstat
            qDebug() << "Server status for listening ok: " << mServer->isListening();
            qDebug() << "Server listen on " << mServer->serverAddress() << ":" << mServer->serverPort();
            connect(mServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
            connect(mServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(socketErr(QAbstractSocket::SocketError)));
            mServerIsInit = true;
            return true;
            qDebug() << "Server status for listening fail" << mServer->isListening();
            delete mServer;
            mServer = nullptr;
            return false;

    Any suggestions to solve that problem with Qt methods only?

  • Moderators

    you can check for windows version during runtime using QSysInfo class:

    QSysInfo::productType() // "windows"
    QSysInfo::productVersion(): // "10"

  • tsocket goes out of scope. Try extending its lifespan

  • Re: How to set QTcpServer to listen exclusivly using Win7

    I found a workaround using winsock2.h to set the QTcpServer listen. After bind I use

    if (listen(sd, SOMAXCONN) == SOCKET_ERROR)
          printf("Listen failed with error: %ld\n", WSAGetLastError());

    That works but I must distinguish on compilation whether it is a Linux or Windows system. That's not so really nice.
    Does anyone has an idea using Qt only to replace workaround code?

  • Lifetime Qt Champion


    Based on the error you get, isn't your QTcpServer instance already ready to work just after you set the socket descriptor ?

    If you need different code path depending on the OS, you have a set defines you can use like Q_OS_WIN.

  • @droid
    [OOI] Are you saying the second listener, when it is there, is another program, or another instance of your own program?

  • @JonB
    The second listener is another program that uses the same port. It can be be started before or during my application is running. There is no other connection allowed on the port when my application is executed.

    The workaround replaces line with method listen in first code example.

Log in to reply