void Connection::connect(QString ip, int port) { //qDebug()<<"Connecting..."<<ip<<port; connectToHost(ip, port); bool res = waitForConnected(1000); //qDebug()<<"result"<<res; }
bool Connection::connectToPeer(QString address, int port) { qDebug() << "connect to " << address << ":" << port; twofish_key = Twofish::generateKey(); this->connectToHost(address, port); if (!waitForConnected(PING_TIMEOUT)) { qDebug() << "failed to connect"; return false; } this->blockSignals(true); QDataStream friend_stream(this); QString request = "REQUEST_CONNECT"; friend_stream << request; friend_stream << name; friend_stream << ownPublicKey; QString s = Rsa::crypt(twofish_key, friendRsaKey).get_str(16).c_str(); friend_stream << s; waitForReadyRead(PING_TIMEOUT); QString answer; friend_stream >> answer; this->blockSignals(false); if (answer == "REQUEST_CONNECT_OK") { twofish = new Twofish(twofish_key, 128); qDebug() << "connected"; return true; } else { qDebug() << "connection failed"; } return false; }
/*! \internal Opens a socket connection to a remote test tool and uses the connection to communicate with the connected tool. */ void QTestRemote::openRemote( const QString &ip, int port ) { connect( ip, port ); if (!waitForConnected(5000)) { // it really doesn't make sense to continue testing if we cannot communicate with the remote tool so we abort immediately. exit( REMOTE_CONNECT_ERROR ); } }
//Permet de renseigner le client avec qui le socket doit communiquer bool Socket::setDescriptor(int socketDescriptor) { if(this->state()==QAbstractSocket::ConnectedState) return false; if(!this->setSocketDescriptor(socketDescriptor)) return false; //On récupère la clé privée du serveur QFile file(PRIVATEKEY_FILE); if(!file.open(QIODevice::ReadOnly)) { qDebug("La clé privée du serveur est introuvable."); return false; } QSslKey key(&file, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "pass"); if (key.isNull()) { qDebug("La clé privée du serveur est nulle"); return false; } file.close(); setPrivateKey(key); //on charge le certificat du client setLocalCertificate( LOCALCERTIFICATE_FILE ); //on charge le certificat de notre ca if(!addCaCertificates(CACERTIFICATES_FILE)) { qDebug("Impossible de charger le certificat du CA."); return false; } //on supprime la vérification des certificats des clients //seuls ces derniers vérifient les clés et certificat du serveur setPeerVerifyMode(QSslSocket::VerifyNone); //on ignore les erreurs car on a un certificat auto signé ignoreSslErrors(); //on se connecte au serveur startServerEncryption(); //On attends au plus 30secondes pour que la connexion s'établisse bool result = waitForConnected(); if(!result) return result; result=waitForEncrypted(); return result; }
bool DebugSession::waitForState(KDevelop::IDebugSession::DebuggerState state, int msecs) { if (!m_connection) return false; if (m_connection->currentState() == state) return true; QTime stopWatch; stopWatch.start(); if (!waitForConnected(msecs)) return false; while (m_connection->currentState() != state) { if (!m_connection) return false; if (!m_connection->socket()) return false; if (!m_connection->socket()->isOpen()) return false; m_connection->socket()->waitForReadyRead(100); if (msecs != -1 && stopWatch.elapsed() > msecs) { return false; } } return true; }
void NetworkPinger::pingOnce() { auto _conn=new QTcpSocket(nullptr); _conn->connectToHost("216.58.219.36", 80); //_conn->connectToHost("74.125.224.83", 80); if (!_conn->waitForConnected(500)) { //qDebug()<<"Not connected" <<_conn->error(); if (!isPaused()) emit connectionHit(false); } else { //qDebug()<<"Connected!"; if (!isPaused()) emit connectionHit(true); } delete _conn; }
CGroupClient::CGroupClient(QByteArray host, int remotePort, QObject *parent) : QTcpSocket(parent) { print_debug(DEBUG_GROUP, "Connecting to remote host..."); setConnectionState(Connecting); print_debug(DEBUG_GROUP, "NOW issuing the connect command..."); connectToHost(host, remotePort); protocolState = AwaitingLogin; linkSignals(); if (!waitForConnected(5000)) { connectionState = CGroupClient::Quiting; close(); print_debug(DEBUG_GROUP, "Server not responding!"); errorHandler(QAbstractSocket::ConnectionRefusedError); //getParent()->changeType(CGroupCommunicator::Off); return; } }
/** * Connect up to the server on a specific port * @param port is the port we're connect to, generally 7777 * @return the port that we're waiting on to connect */ bool Client::Connect(unsigned port) { connectToHost(QHostAddress::LocalHost, 7777); return waitForConnected(1000); }