Cant connect to a Server
-
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.
-
Hi
Can be bug
https://bugreports.qt.io/browse/QTBUG-22876
Can be bug in your codeWe 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?
-
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.
-
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.
-
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.
-
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/