void clientModel::connecttoServ(QString i, QString p, QString n) { ipAddr = i; portNumb = p; userName = n; secureSocket->abort(); secureSocket->setPeerVerifyMode(QSslSocket::QueryPeer); secureSocket->connectToHostEncrypted(ipAddr, p.toInt()); connect(secureSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSSLError(QList<QSslError>))); connect(secureSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); if (!secureSocket->waitForEncrypted(1000)) { emit showWindows(4); return; } QString s = userName; secureSocket->write(s.toUtf8().constData(), s.length()); while(!secureSocket->waitForReadyRead()) { } QString t = secureSocket->readLine(); if (t == "NO\n") { emit showWindows(0); secureSocket->disconnect(); } else if (t == "YES\n") { emit showWindows(1); QString names = secureSocket->readLine(); QStringList friendsList; while(names != "//FINISHED//\n") { friendsList << names; names = secureSocket->readLine(); } emit updateFriends(friendsList); connect(secureSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); } }
Client::Client(QWidget *parent) : QDialog(parent) { hostLabel = new QLabel(tr("&Server name:")); portLabel = new QLabel(tr("S&erver port:")); nameLabel = new QLabel(tr("&Client name:")); // Find out which IP to connect to QString ipAddress; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // Use the first non-localhost IPv4 address for (int i = 0; i < ipAddressesList.size(); ++i) { if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) { ipAddress = ipAddressesList.at(i).toString(); break; } } // If we did not find one, use IPv4 localhost if (ipAddress.isEmpty()) ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); hostLineEdit = new QLineEdit(ipAddress); portLineEdit = new QLineEdit; clientNameLineEdit = new QLineEdit; portLineEdit->setValidator(new QIntValidator(1, 65535, this)); hostLabel->setBuddy(hostLineEdit); portLabel->setBuddy(portLineEdit); nameLabel->setBuddy(clientNameLineEdit); connectButton = new QPushButton(tr("Connect")); connectButton->setDefault(true); connectButton->setEnabled(false); // Make the quit button exit chat sessions quitButton = new QPushButton(tr("Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(connectButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); secureSocket = new QSslSocket(this); // Special slot to handle errors with the certificates // in particular the fact that they are self-signed connect(secureSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSSLError(QList<QSslError>))); // Connect signals to slots for the chat window connect(hostLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableConnectButton())); connect(portLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableConnectButton())); connect(clientNameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableConnectButton())); connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToChatServer())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(secureSocket, SIGNAL(readyRead()), this, SLOT(messageComing())); connect(secureSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(hostLabel, 0, 0); mainLayout->addWidget(hostLineEdit, 0, 1); mainLayout->addWidget(portLabel, 1, 0); mainLayout->addWidget(portLineEdit, 1, 1); mainLayout->addWidget(nameLabel, 2, 0); mainLayout->addWidget(clientNameLineEdit, 2, 1); mainLayout->addWidget(buttonBox, 3, 0, 1, 2); setLayout(mainLayout); QVBoxLayout* vlay = new QVBoxLayout(); vlay->addWidget(&clientChatLog); vlay->addWidget(&clientMessage); closeChatWindow.setText("Quit"); vlay->addWidget(&closeChatWindow); chatWindow.setLayout(vlay); connect(&clientMessage, SIGNAL(returnPressed()), this, SLOT(sendMessage())); connect(&userList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(clientSelected(QListWidgetItem*))); setWindowTitle(tr("Secure Chatp2p Client")); portLineEdit->setFocus(); connect(&closeChatWindow, SIGNAL(clicked()), this, SLOT(closeChat())); }
void clientController::on_tryConnect(QString server, QString port, QString name) { m_secureSocket = new QSslSocket(this); // special slot to handle errors with the certificates // in particular the fact that they are self-signed connect(m_secureSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSSLError(QList<QSslError>))); m_secureSocket->connectToHostEncrypted(server, port.toInt()); if (!m_secureSocket->waitForEncrypted(3000)){ QMessageBox::critical(m_w, "ERROR", "Error: Couldn't connect to host"); return; } //displayCertificateWindow(); if(name.isEmpty()) name=m_secureSocket->peerAddress().toString(); QString msg = "0\n" + name + "\n"; m_secureSocket->write(msg.toUtf8()); if(!m_secureSocket->waitForReadyRead(15000)){ QMessageBox timeOut; timeOut.setText("The server timed out on this request try again"); timeOut.exec(); return; } //qDebug() << client->readAll();fromUtf8(client->readLine()).trimmed() bool isValid = false; int state = QString::fromUtf8(m_secureSocket->readLine()).trimmed().toInt(&isValid,10); if (!isValid){ qDebug() << "first readline is not a number for the namecheck"; } if(state == 4){//on error; QMessageBox dupName; dupName.setText(m_secureSocket->readLine().trimmed()); dupName.exec(); return; } else if(state == 0){//on success join m_w->close(); m_secureSocket->flush(); m_clientName = name; msg = m_secureSocket->readLine(); if(!msg.trimmed().compare("1")){ msg = m_secureSocket->readLine(); QStringList names = msg.split(","); names.last().remove('\n'); emit userListChanged(names); } connect(m_secureSocket, SIGNAL(readyRead()), this, SLOT(readyRead())); m_userWindow->setWindowTitle(name.trimmed() + "'s user list"); m_userWindow->show(); } else qDebug()<<"a bad state was received trying to connect\n"; }
Client::Client(QWidget *parent) : QDialog(parent) { hostLabel = new QLabel(tr("&Server name:")); portLabel = new QLabel(tr("S&erver port:")); // find out which IP to connect to QString ipAddress; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // use the first non-localhost IPv4 address for (int i = 0; i < ipAddressesList.size(); ++i) { if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) { ipAddress = ipAddressesList.at(i).toString(); break; } } // if we did not find one, use IPv4 localhost if (ipAddress.isEmpty()) ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); hostLineEdit = new QLineEdit(ipAddress); portLineEdit = new QLineEdit; portLineEdit->setValidator(new QIntValidator(1, 65535, this)); hostLabel->setBuddy(hostLineEdit); portLabel->setBuddy(portLineEdit); statusLabel = new QLabel(tr("This examples requires that you run the " "Secure Fortune Server example as well.")); getFortuneButton = new QPushButton(tr("Get Fortune")); getFortuneButton->setDefault(true); getFortuneButton->setEnabled(false); quitButton = new QPushButton(tr("Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); secureSocket = new QSslSocket(this); // special slot to handle errors with the certificates // in particular the fact that they are self-signed connect(secureSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSSLError(QList<QSslError>))); connect(hostLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableGetFortuneButton())); connect(portLineEdit, SIGNAL(textChanged(QString)), this, SLOT(enableGetFortuneButton())); connect(getFortuneButton, SIGNAL(clicked()), this, SLOT(requestNewFortune())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(secureSocket, SIGNAL(readyRead()), this, SLOT(readFortune())); connect(secureSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(hostLabel, 0, 0); mainLayout->addWidget(hostLineEdit, 0, 1); mainLayout->addWidget(portLabel, 1, 0); mainLayout->addWidget(portLineEdit, 1, 1); mainLayout->addWidget(statusLabel, 2, 0, 1, 2); mainLayout->addWidget(buttonBox, 3, 0, 1, 2); setLayout(mainLayout); setWindowTitle(tr("Secure Fortune Client")); portLineEdit->setFocus(); }