Server-> nextPendingConnection() need some help



  • Hi all... I need some help with type casting or any thing which helps me to solve this problem.
    I have a QTcpServer, and I connected the newConnection() signal of it to some slot. As you can see in the code below I used QTcpServer::nextPendingConnection() to get the new connection. but this method returns a QTcpSocket, And instead I need a QSslSocket for the rest of my program. Is it any possible way, for example like static_cast or ... to make the QTcpSocket to a QSslSocket ..!? I know that QSslSocket inherits QTcpSocket

    @

    void Server::manageQuery()
    {
    QTcpServer *myServer = qobject_cast<QTcpServer*>(sender());
    QTcpSocket *socket = myServer->nextPendingConnection();
     
    connect(socket,SIGNAL(readyRead()),this,SLOT(processQuery()));
    connect(socket,SIGNAL(disconnected()),socket,SLOT(deleteLater()));
    }
    

    @

    I mean some code like this

    @
    void Server::manageQuery()
    {
    QTcpServer myServer = qobject_cast<QTcpServer>(sender());
    QTcpSocket *socket = (QSslSocket)myServer->nextPendingConnection();

    connect(socket,SIGNAL(readyRead()),this,SLOT(processQuery()));
    connect(socket,SIGNAL(disconnected()),socket,SLOT(deleteLater()));
    }
    

    @

    can make the QTcpSocket to QSslSocket? or not ....



  • The cast is like this :

    @
    QTcpSocket *socket = qobject_cast<QSslSocket *>myServer->nextPendingConnection();
    @

    See also : static_cast and dynamic_cast.

    if the cast does not work you can use :

    @
    void QTcpServer::incomingConnection ( int socketDescriptor )
    @

    instead of QTcpServer::nextPendingConnection () !



  • bq. The cast is like this :

    QTcpSocket *socket = qobject_cast<QSslSocket *>myServer->nextPendingConnection();
    

    See also : static_cast and dynamic_cast.

    if the cast does not work you can use :bq.

    none of them works.

    bq. void QTcpServer::incomingConnection ( int socketDescriptor )

    instead of QTcpServer::nextPendingConnection () ! bq.
    I also know this but I need to use nextPendingConnection () !



  • Hi. If the cast does not work, that means you should find another way to accept the connection instead of using nextPendingConnection() !

    " helpful doc":http://dgraves.org/content/qt-notes-working-qsslsocket



  • I just needed the same functionally and come up with this solution. I didn't test it however the doc and lots of sources says that this is the way to provide SSl functionality to QTcpSocket.

    Firs you have to reimplement the virtual function incomingConnection():

    @/* This virtual function is normally called by QTcpServer when a new connection

    • is available. The base implementation creates a QTcpSocket, sets the socket
    • descriptor and then stores the QTcpSocket in an internal list of pending
    • connections. Finally newConnection() is emitted.
    • However we'll reimplement this virtual function to provide SSL
    • functionality. /
      void Server::incomingConnection( int socketDescriptor )
      {
      QSslSocket
      socket = new QSslSocket(this);
      socket->setProtocol(QSsl::SslV2); // TODO: Or SslV3?
      // For now these files should be reside in the same folder as the application
      socket->setPrivateKey("server.key"); // $ openssl genrsa -out server.key 2048
      socket->setLocalCertificate("server.crt"); // $ openssl req -new -x509 -key server.key -out server.crt -days 1095

    if (socket->setSocketDescriptor(socketDescriptor)) {
    // pendingConnections is a QQueue<QSslSocket*>
    this->pendingConnections.enqueue(socket);
    // newConnection is emmited within QTcpServerPrivate. No need to emit it manually.
    connect(socket, SIGNAL(encrypted()), this, SLOT(ready()));
    socket->startServerEncryption(); // Initiate the SSL handshake
    } else {
    delete socket;
    }
    }@

    As you see we are adding our newly created QSslSocket to the QQueue data type(create it before). Now we use this queue within the nextPendingConnection() function:

    @/* Use our own list (QQueue pendingConnections) instead of QTcpServer's

    • internal list. Needed for SSL functionality. /
      QTcpSocket
      Server::nextPendingConnection()
      {
      // Return the next connection in the queue. If there is none,
      // return 0.
      if (this->pendingConnections.isEmpty()) {
      return 0;
      } else {
      return this->pendingConnections.dequeue();
      }
      }@

    Also the ready() slot is just for debugging or some other task. It's up to you. There lots of things still to do, however i think this is a good point to start.



  • This is it farslan ! It seems to be working ;)


Log in to reply
 

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