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. Cant connect to a Server
Forum Updated to NodeBB v4.3 + New Features

Cant connect to a Server

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 3.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a QTcpServer that is listening, when i start him. I checked my System and he listens on localhost on port 6111. In my Server i have overwritten the incomingConnection Function. But i cant connect with my Client. My Client always says this

    qt.network.ssl: QSslSocket: cannot resolve SSLv3_client_method
    qt.network.ssl: QSslSocket: cannot resolve SSLv3_server_method
    QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()
    "Network operation timed out"
    

    I use QSslSocket and for testing i have insert a qDebug() << "connection"; to my incomingConnection function that i have overwritten. So the Problem is maybe that the Server is not getting any Connection from my Client. Anyone has some Ideas? If more Informations are necessary please ask.

    Dont know where i start to find the Error. As i said my Server is listening, but i cant go for a connection.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Can be bug
      https://bugreports.qt.io/browse/QTBUG-22876
      Can be bug in your code

      We need more information to give any guesses.

      Like platform. Qt version and maybe some code.

      Also, the server and client RUNS on same machine, correct?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        Both Programs are running on the same Machine. I use Manjaro based on Arch Linux with KDE. edit: I forgot to say that im using 5.7

        My Server Class

        void Server::start()
        {
            if (this->listen(QHostAddress::LocalHost, 6111))
                qDebug() << "Server started!";
            else
                qDebug() << "Server couldnt get started!";
        
        }
        
        void Server::incomingConnection(int socketDescriptor)
        {
            qDebug() << "Incoming Connection";
            QScopedPointer<ConnectionThread> thread(new ConnectionThread(socketDescriptor, this));
        
            connect(thread.data(), SIGNAL(finished()), thread.data(), SLOT(deleteLater()));
            thread->start();
        }
        

        The QThread Class (only the importan Parts)

        void ConnectionThread::run()
        {
            _socket.reset(new QSslSocket());
        
            if (!_socket->setSocketDescriptor(this->_socketDescriptor))
            {
                emit error(_socket->error());
            }
        
            //_socket->setPrivateKey(":/cert/key.pem");
            //_socket->setLocalCertificate(":/cert/cert.pem");
            _socket->startServerEncryption();
        
            connect(_socket.data(), SIGNAL(readyRead()), this, SLOT(handleConnection()));
            connect(_socket.data(), SIGNAL(disconnected()), this, SLOT(disconnected()));
            connect(_socket.data(), SIGNAL(sslErrors(const QList &)), this, SLOT(errorSsl(QList)));
        
            exec();
        }
        
        void ConnectionThread::handleConnection()
        {
            if (_socket->waitForEncrypted())
            {
                QScopedPointer<User> user(new User());
        
                if (_socket->waitForReadyRead())
                {
                    QString line = _socket->readAll();
                    user->parse(line);
        
                    QByteArray bytes;
                    bytes.append(user->getAnswer());
                    _socket->write(bytes);
                    if (_socket->waitForBytesWritten())
                        _socket->disconnect();
                }
            }
        }
        

        And my Client Connection function

        void Network::sendData( const QString &data, QString *response)
        {
            QScopedPointer<QSslSocket> socket(new QSslSocket());
        
            socket->connectToHostEncrypted(HOST, PORT);
        
            QList<QSslError> errors = socket->sslErrors();
            for (int i = 0; i < errors.size(); ++i)
                qDebug() << errors.at(i).errorString();
        
            socket->ignoreSslErrors();
        
            if (socket->waitForEncrypted())
            {
                QByteArray bytes;
                bytes.append(data);
                socket->write(bytes);
                if (socket->waitForBytesWritten())
                {
                    if (socket->waitForReadyRead())
                    {
                        *response = socket->readAll();
                        if (socket->disconnect())
                            socket->disconnectFromHost();
                    }
                        else
                        *response = socket->errorString();
                }
                else
                    *response = socket->errorString();
            }
            else
                *response = socket->errorString();
        }
        

        I dont understand why my Server dont ouput this qDebug() << "Incoming Connection"; . I guess there is no incoming Connection, but i dont know why.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bludger
          wrote on last edited by VRonin
          #4

          Hi,

          To answer your question about why your server isn't printing "incoming connection".
          I think you're not correctly overriding the virtual protected incomingConnection function. Your function signature differs from the one of QTcpServer, check out: http://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
          You're using an int but it should be a qintptr instead.

          About the SSL problems:

          • do you have OpenSSL installed on your system?
          • what version of OpenSSL are you using? I'm not sure when but lately OpenSSL changed some things which could result in the error(s) you've.
          1 Reply Last reply
          2
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Wow nice. You was right. I read the documentaion then i searched a Tutorial and in the Tutorial they used int instead of qintptr.

            The Solution was

            void incomingConnection(qintptr socketDescriptor);
            

            Thx again.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              There is so much wrong I don't know where to start. As mentioned above the signature is wrong, use Q_DECL_OVERRIDE to spot these mistakes

              • The lifecycle of variables is wrong (QScopedPointer deletes everything when it goes out of scope)
              • some connections are wrong ( SIGNAL(sslErrors(const QList &)) missing template parameter)
              • after socket->connectToHostEncrypted(HOST, PORT); you do not wait for connected
              • socket->disconnect() does not do what you think it does (that disconnects signals and slots, not the network)
              • waitForReadyRead() waits untill some data can be read, there is no assurance all data can be read
              • QString line = _socket->readAll(); and bytes.append(data); does not support unicode (you should use QDataStream)
              • There is no way I can see to stop the thread

              Minor:

              • using syncronous waits is evil!
              • you did threading wrong, see https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

              "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

              • Login

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