[SOLVED] GUI Client <-> Console Server : Data(string) transfer
-
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 USERds >> 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;
}
}
}
@ -
By taking a short look at the code I wasn't able to see your problem with the socket itself.
However I have spottet a logic error. If you SELECT from the database, like you do it, then the usernamen and password need to match and query.next() should return your usr entry.
But if one of that is not true, your code never enters the while loop and will also never return anything.
So I suggest something like this.- query only the username
- if (!query.next()) return "no" (=> user does not exist)
- else check the password with an extra query (you should change that to a md5 hash or something similar, and just compare the hashes by the way)
As a sidenode I would suggest adding a size var to your tcp message block and wait for all bytes received befor you read your data. I do that this way:
Sending a message
@qint64 send(const QString &msg) {
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_0);out << (quint64)0; // size of message out << msg; out.device()->seek(0); out << (quint64)(block.size() - sizeof(quint64)); return socket->write(block);
}@
Receiving (readyRead())
@void readMesssage() {
QDataStream in(socket);
in.setVersion(QDataStream::Qt_5_0);if (blockSize == 0) { if (socket->bytesAvailable() < (int)sizeof(quint64)) return; in >> blockSize; } if (socket->bytesAvailable() < (qint64)blockSize) return; QString message; in >> message; blockSize = 0; emit message(msg); // more data avaliable => read recursive if (socket->bytesAvailable() >= (int)sizeof(quint64)) readMesssage();
}@
-
Thank you :)
I will send 4 values to the Server: username, password, IP Address and MAC ID of the machine from where the Client is running. How can I authenticate correctly for the valid user ? In looping,
@
if (username == true)if(password == true)
if(IP == true) if(MAC == true)
else
SOCKET.CLOSE();
@
Is this right? -
Hi qxoz, this is my Server code:
@#include "server.h" //server.cpp
#include <iostream>
#define STR_EQUAL 0
#define STR_UNEQ 1using namespace std;
Server::Server()
{
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::Any,3005)) 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);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
QString text,text1,ip,mac;
ds >> text;
ds >> text1;
ds >> mac;
ds >> ip;
qDebug() << "User Name is " + text;
qDebug() << "Password is " + text1;
qDebug() << "IP Address is " + ip;
qDebug() << "MAC Address is " + mac;db.setHostName("sri-local");
db.setDatabaseName("guests");
db.setUserName("sri");
db.setPassword("sri");
db.open();QSqlQuery query;
query.exec("Select * from visitor where EML='" + text + "' AND PSW = '" + text1 +
"' AND IP='" + ip + "' AND MAC='" + mac + "'");while(query.next())
{
QString EML = query.value(0).toString();
QString PSW = query.value(1).toString();
QString MAC = query.value(2).toString();
QString IP = query.value(3).toString();if(QString::compare(text,EML) == STR_EQUAL && QString::compare(text1,PSW) == STR_EQUAL &&
QString::compare(ip,IP) == STR_EQUAL && QString::compare(mac,MAC) == STR_EQUAL)
{
QString conf = "yes";
clientConnection->write(conf.toAscii());
clientConnection->flush();
//db.close();
//QSqlDatabase::removeDatabase("QMYSQL");
}else
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
//out.setVersion(QDataStream::Qt_5_0);out << (quint64)0; // size of message out << "no"; out.device()->seek(0); out << (quint64)(block.size() - sizeof(quint64)); clientConnection->write(block);
//QString conf2 = "no";
//clientConnection->write(conf2.toAscii());
clientConnection->flush();
}
}
}
@As u said, @if(username && password && IP && MAC){
cout<< "true user";
socket.write("yes"); // to send Client a signal that he is valid and can continue with other operations in the Client app.
}@Upto this I can send and validate the user.
But when the user credentials or IP or MAC is wrong, I am not able to warn user that he is an invalid user, I mean,
@else{
cout<<"invalid user";
socket.write("No"); // not able to send this signal to user.
}@ -
Look at your Sql query:
@query.exec("Select * from visitor where EML='" + text + "' AND PSW = '" + text1 +
"' AND IP='" + ip + "' AND MAC='" + mac + "'");@
you already select neccessary user and if some of parameters are wrong you wont get any record from DB. After query you not need check parameters again.
@if(query.next()){
cout<< "true user";
socket.write("yes");
} else{
cout<<"invalid user";
socket.write("No");
}@i am not clearly understand:
"But when the user credentials or IP or MAC is wrong, I am not able to warn user that he is an invalid user"