title: How set QTcpServer listening alone on port using Win7
body: <p>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:</p>
  
  <pre><code>    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() &lt;&lt; "Socket bind fails";
      }
      else
      {
          qDebug() &lt;&lt; "Socket bind success";
      }
      sd = tsocket.socketDescriptor(); // valid socket descriptor
  
      if (!mServer-&gt;setSocketDescriptor(sd))
      {
          qDebug() &lt;&lt; "SocketDescriptor fails";
      }
  
      sd = mServer-&gt;socketDescriptor();
      qDebug() &lt;&lt; "Socketdescriptor Server " &lt;&lt; sd;
  //end tries to set socket properties to a non sharing port
  
      if (mServer-&gt;listen(QHostAddress(ipAddress), port)) // fails with message ... is always listening
  //  if (mServer-&gt;mServer-&gt;isListening()) // is not listening tells netstat
      {
          qDebug() &lt;&lt; "Server status for listening ok: " &lt;&lt; mServer-&gt;isListening();
          qDebug() &lt;&lt; "Server listen on " &lt;&lt; mServer-&gt;serverAddress() &lt;&lt; ":" &lt;&lt; mServer-&gt;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() &lt;&lt; "Server status for listening fail" &lt;&lt; mServer-&gt;isListening();
          delete mServer;
          mServer = nullptr;
          return false;
      }
  }
  </code></pre>
  
  <p>Thanks for any idea how to set the socket options for a exclusive usage of the listening port.</p>
  
  <p>Martin</p>