Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set QTcpServer to listen exclusivly using Win7
Forum Updated to NodeBB v4.3 + New Features

How to set QTcpServer to listen exclusivly using Win7

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 995 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    droid
    wrote on last edited by
    #1

    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";
        }
        else
        {
            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;
        }
        else
        {
            qDebug() << "Server status for listening fail" << mServer->isListening();
            delete mServer;
            mServer = nullptr;
            return false;
        }
    }
    

    Any suggestions to solve that problem with Qt methods only?

    raven-worxR 1 Reply Last reply
    0
    • D droid

      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";
          }
          else
          {
              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;
          }
          else
          {
              qDebug() << "Server status for listening fail" << mServer->isListening();
              delete mServer;
              mServer = nullptr;
              return false;
          }
      }
      

      Any suggestions to solve that problem with Qt methods only?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

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

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        tsocket goes out of scope. Try extending its lifespan

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • D Offline
          D Offline
          droid
          wrote on last edited by
          #4

          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());
                closesocket(sd);
                WSACleanup();
              }
          

          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?

          JonBJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • D droid

              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());
                    closesocket(sd);
                    WSACleanup();
                  }
              

              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?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                droid
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved