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. Server-> nextPendingConnection() need some help
Forum Updated to NodeBB v4.3 + New Features

Server-> nextPendingConnection() need some help

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 8.2k Views 1 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.
  • C Offline
    C Offline
    CuteKQ
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • I Offline
      I Offline
      issam
      wrote on last edited by
      #2

      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 () !

      http://www.iissam.com/

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CuteKQ
        wrote on last edited by
        #3

        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 () !

        1 Reply Last reply
        0
        • I Offline
          I Offline
          issam
          wrote on last edited by
          #4

          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

          http://www.iissam.com/

          1 Reply Last reply
          0
          • F Offline
            F Offline
            farslan
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              issam
              wrote on last edited by
              #6

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

              http://www.iissam.com/

              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