Hi, I am using the following Client and Server for information passing, I am able to send information from Client to Server, but unable to receive some data from Server.
Here’s my Client:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QTcpSocket>
#include <QHostAddress>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::validate(){
tcpSocket = new QTcpSocket(this);
ds.setDevice(tcpSocket);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(displayIncoming()));
tcpSocket->abort();
tcpSocket->connectToHost(QHostAddress("127.0.0.1"),8002);
if (tcpSocket->waitForConnected(1000)){
QString text = ui->lineEdit->text();
QString text1 = ui->lineEdit_2->text();
ds << text;
ds << text1;
}
//QMessageBox::information(this,"OK","You have clicked OK button");
}
void MainWindow::displayIncoming(){
QTextStream cin(stdin); // I NEED TO RECEIVE ONE CONFIRMATION STRING HERE FROM SERVER TO //AUTHENTICATE THE USER
ds >> text3;
if(text3.toStdString() == "yes") {
QMessageBox::information(this,"Valid User","You are a valid user");
}
else{
QMessageBox::information(this,"Invalid User","You are an invalid user");
}
// ds >> text1;
// cout << text.toStdString()<<endl;
// cout << text1.toStdString()<<endl;
// text = cin.readLine();
// text1 = cin.readLine();
// ds << text;
// ds << text1;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
Here’s is my Server:
#include "server.h" //server.cpp
#include <iostream>
//#include <QSqlDatabase>
//#include <QSqlQueryModel>
//#include <QSqlQuery>
#define STR_EQUAL 0
using namespace std;
Server::Server()
{
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::Any,8002)) cout << "Unable to start the server." <<endl;
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(getConnection()));
}
void Server::getConnection()
{
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(echoText()));
connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
}
void Server::echoText()
{
QDataStream ds(clientConnection);
QString text,text1;
QString suc;
ds >> text;
ds >> text1;
qDebug() << "User Name is " + text;
qDebug() << "Password is " + text1;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("sri-local");
db.setDatabaseName("guests");
db.setUserName("sri");
db.setPassword("sri");
db.open();
QSqlQuery query;
query.exec("Select * from usr where EML='" + text + "' AND PSW = '" + text1 + "'");
while(query.next()){
QString EML = query.value(0).toString();
QString PSW = query.value(1).toString();
//if (EML==text && PSW==text1)
if(QString::compare(text,EML) == STR_EQUAL || QString::compare(text1,PSW) == STR_EQUAL)
{ // IF ITS TRUE, I NEED TO SEND "yes" TO CLIENT, SO THAT HE IS VALID
text = "yes";
ds << text;
//clientConnection->waitForBytesWritten(1000);
qDebug() << text;
}
else{
text = "From Server: " + text;
text1 = "From Server: " + text1;
//qDebug() << "To Client:" + text;
//qDebug() << "To Client:" + text1;
ds << text;
ds << text1;
}
}
}
↧