Server::
void FortuneThread::run()
{
if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket->error());
return;
}
qDebug()<<"Here Connection is established.. as newConnection signal is emitted";
createConnection();
model->setTable("Users");
model->select();
tcpSocket->write("Hello from Server to Client");
tcpSocket->waitForBytesWritten(10000);
connect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1()),Qt::DirectConnection);
tcpSocket->waitForReadyRead(10000);
}
void FortuneThread::ready1()
{
char buf[1024];
qint64 lineLength = tcpSocket->read(buf, sizeof(buf));
if (lineLength != -1) {
// the line is available in buf
}
printf("The rec string in threaded Server = %s\n", buf);
fflush(0);
if(s1 == "TotalUsers")
{
/* total Users */
qDebug()<<"I am in ready1";
QSqlQuery q1
("select COUNT(UserID) as totalcount from Users");
int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
while (q1.next())
j1 = q1.value(nameColl).toString();//j1 = q1.value(nameColl).toInt();
/* total Users */
qDebug()<<"Total Number of Users are: j1 = :"<<j1;
/*char buf[30] = "Total Number of Users are: j1";
tcpSocket->write(buf, sizeof(buf));*/
tcpSocket->write(("Total Number of Users are :" + j1 ).toAscii().data());
tcpSocket->waitForBytesWritten(10000);
}else if(s1 == "UserInfo")
{
static int a;
/* total Users */
QSqlQuery q1
("select COUNT(UserID) as totalcount from Users");
int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
int j1;
while (q1.next())
j1 = q1.value(nameColl).toInt();
/* total Users */
QString j, k, l, m, jarr
[j1
], karr
[j1
], larr
[j1
], marr
[j1
], all_data;
int nameCol = rec.indexOf("UserID"); // index of the field "UserID"
int nameCol1 = rec.indexOf("UserName");
int nameCol2 = rec.indexOf("Verification");
int nameCol3 = rec.indexOf("PIN");
int i = 0;
while (q.next())
{
j = q.value(nameCol).toString();
k = q.value(nameCol1).toString();
l = q.value(nameCol2).toString();
m = q.value(nameCol3).toString();
qDebug() << "USerID = : " << j; // output number of records//qDebug consol o/p
qDebug() << "UserName = : " << k;
qDebug() << "Verification Method: " << l;
qDebug() << "PIN = : " << m;
jarr[i] = j;
karr[i] = k;
larr[i] = l;
marr[i] = m;
i++;
}
for( i = 0; i < j1; i++)
{
all_data += jarr[i] + "\t" + karr[i] + "\t" + larr[i] + "\t" + marr[i] + "\n";
}
tcpSocket->write((all_data).toAscii().data());//this statement writes the string to the socket
tcpSocket->waitForBytesWritten(10000);
}else if(s1 == "adduser/117/Manoj/FC/weqeqwe")
{
bool i;
int m=0;
/*QString userid = "117";
QString username = "Manoj";
QString verification = "FC";
QString pin = "weqeqwe";*/
QString userid
= s1.
section('/',
1,
1);
QString username
= s1.
section('/',
2,
2);
QString verification
= s1.
section('/',
3,
3);
QString pin
= s1.
section('/',
4,
4);
qDebug()<< "userid in server.cpp = " << userid;
qDebug()<< "username in server.cpp = " << username;
qDebug()<< "verification in server.cpp = " << verification;
qDebug()<< "pin in server.cpp = " << pin;
QString stat
= QString("insert into Users values('%0', '%1' ,' %2',' %3')").
arg(userid
).
arg(username
).
arg(verification
).
arg(pin
);
i = query.exec(stat);
m = query.size();
printf("\n number of rows = %d\n", m);
if(!i)
{
QString message
(tr
("User is not addedd 1:User already exists 2:user id should be a number"));
qDebug()<<"User Not Added";
}else
{
QString message
(tr
("User added Successfully. "));
qDebug()<<"User Added";
}
}else if(s1 == "edit user 117 Manoj FC ManojPIN")
{
int i;
query.prepare("update Users set UserName = ?,Verification = ?,PIN = ? where UserID = ?");
query.addBindValue(username);
query.addBindValue(verification);
query.addBindValue(pin);
query.addBindValue(userid);
query.exec();
i = query.numRowsAffected();
if(i)
{
QString message
(tr
("User updated Successfully. "));
}else
{
qDebug()<<"UserID does not exist, cannot be edited.";
}
}else if(s1 == "delete user 117 Manoj FC ManojPIN")
{
QString message
(tr
("User Deleted Successfully"));
query.exec(string);
tcpSocket->write("UserID 117 Deleted Successfully");
qDebug()<<"User Deleted";
}
}
client.cpp
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(requestNewFortune()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
this->setWindowTitle("Client");
}
void Client::requestNewFortune()
{
blockSize = 0;
tcpsocket->abort();
tcpsocket->connectToHost(ui->lineEdit->text(), ui->lineEdit_2->text().toInt());
connect(this->tcpsocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(ui->AddUserBtn, SIGNAL(clicked()), SLOT(sendUserDetails()));
connect(ui->EditUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoEdit()));
connect(ui->DeleteUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoDelete()));
connect(ui->TotalUserBtn, SIGNAL(clicked()), SLOT(GetTotalNumberofUsers()));
connect(ui->GetUserInfoBtn, SIGNAL(clicked()), SLOT(GetUserInfo()));
connect(ui->disconnect_Btn, SIGNAL(clicked()), SLOT(DisconnectFromServer()));
}
void Client::readFortune()
{
char cbuf[1024];
qint64 lineLength = tcpsocket->read(cbuf, sizeof(cbuf));
if (lineLength != -1) {
// the line is available in buf
}
ui->label_3->setText(cbuf);
}
{
switch (socketError) {
.....
}
}
void Client::sendUserDetails()
{
AddUser *idadduser = new AddUser(tcpsocket);
idadduser->exec();
}
void Client::GetTotalNumberofUsers()
{
char buf[12] = "TotalUsers";
qint64 lineLength = tcpsocket->write(buf, sizeof(buf));
}
void Client::GetUserInfo()
{
char buf[12] = "UserInfo";
tcpsocket->write(buf, sizeof(buf));
}
void Client::sendUserDetailstoEdit()
{
......
}
void Client::sendUserDetailstoDelete()
{
........
}
void Client::DisconnectFromServer()
{
tcpsocket->close();
}
Server::
void FortuneThread::run()
{
tcpSocket = new QTcpSocket(this);
if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket->error());
return;
}
qDebug()<<"Here Connection is established.. as newConnection signal is emitted";
createConnection();
model = new QSqlTableModel(this);
model->setTable("Users");
model->select();
tcpSocket->write("Hello from Server to Client");
tcpSocket->waitForBytesWritten(10000);
connect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1()),Qt::DirectConnection);
tcpSocket->waitForReadyRead(10000);
}
void FortuneThread::ready1()
{
char buf[1024];
qint64 lineLength = tcpSocket->read(buf, sizeof(buf));
if (lineLength != -1) {
// the line is available in buf
}
printf("The rec string in threaded Server = %s\n", buf);
fflush(0);
QString s1 = buf;
if(s1 == "TotalUsers")
{
/* total Users */
qDebug()<<"I am in ready1";
QSqlQuery q1("select COUNT(UserID) as totalcount from Users");
QSqlRecord rec1 = q1.record();
int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
QString j1;//int j1;
while (q1.next())
j1 = q1.value(nameColl).toString();//j1 = q1.value(nameColl).toInt();
/* total Users */
qDebug()<<"Total Number of Users are: j1 = :"<<j1;
/*char buf[30] = "Total Number of Users are: j1";
tcpSocket->write(buf, sizeof(buf));*/
tcpSocket->write(("Total Number of Users are :" + j1 ).toAscii().data());
tcpSocket->waitForBytesWritten(10000);
}else if(s1 == "UserInfo")
{
static int a;
/* total Users */
QSqlQuery q1("select COUNT(UserID) as totalcount from Users");
QSqlRecord rec1 = q1.record();
int nameColl = rec1.indexOf("totalcount"); // index of the field "name"
int j1;
while (q1.next())
j1 = q1.value(nameColl).toInt();
/* total Users */
QString j, k, l, m, jarr[j1], karr[j1], larr[j1], marr[j1], all_data;
QSqlQuery q("Select * from Users");
QSqlRecord rec = q.record();
int nameCol = rec.indexOf("UserID"); // index of the field "UserID"
int nameCol1 = rec.indexOf("UserName");
int nameCol2 = rec.indexOf("Verification");
int nameCol3 = rec.indexOf("PIN");
int i = 0;
while (q.next())
{
j = q.value(nameCol).toString();
k = q.value(nameCol1).toString();
l = q.value(nameCol2).toString();
m = q.value(nameCol3).toString();
qDebug() << "USerID = : " << j; // output number of records//qDebug consol o/p
qDebug() << "UserName = : " << k;
qDebug() << "Verification Method: " << l;
qDebug() << "PIN = : " << m;
jarr[i] = j;
karr[i] = k;
larr[i] = l;
marr[i] = m;
i++;
}
for( i = 0; i < j1; i++)
{
all_data += jarr[i] + "\t" + karr[i] + "\t" + larr[i] + "\t" + marr[i] + "\n";
}
tcpSocket->write((all_data).toAscii().data());//this statement writes the string to the socket
tcpSocket->waitForBytesWritten(10000);
}else if(s1 == "adduser/117/Manoj/FC/weqeqwe")
{
bool i;
int m=0;
QSqlQuery query;
/*QString userid = "117";
QString username = "Manoj";
QString verification = "FC";
QString pin = "weqeqwe";*/
QString userid = s1.section('/', 1,1);
QString username = s1.section('/', 2,2);
QString verification = s1.section('/', 3,3);
QString pin = s1.section('/', 4,4);
qDebug()<< "userid in server.cpp = " << userid;
qDebug()<< "username in server.cpp = " << username;
qDebug()<< "verification in server.cpp = " << verification;
qDebug()<< "pin in server.cpp = " << pin;
QString stat = QString("insert into Users values('%0', '%1' ,' %2',' %3')").arg(userid).arg(username).arg(verification).arg(pin);
i = query.exec(stat);
m = query.size();
printf("\n number of rows = %d\n", m);
if(!i)
{
QString message(tr("User is not addedd 1:User already exists 2:user id should be a number"));
qDebug()<<"User Not Added";
}else
{
QString message(tr("User added Successfully. "));
qDebug()<<"User Added";
}
}else if(s1 == "edit user 117 Manoj FC ManojPIN")
{
int i;
QSqlQuery query;
QString userid = "117";
QString username = "Manoj";
QString verification = "FC";
QString pin = "ManojPIN";
query.prepare("update Users set UserName = ?,Verification = ?,PIN = ? where UserID = ?");
query.addBindValue(username);
query.addBindValue(verification);
query.addBindValue(pin);
query.addBindValue(userid);
query.exec();
i = query.numRowsAffected();
if(i)
{
QString message(tr("User updated Successfully. "));
}else
{
qDebug()<<"UserID does not exist, cannot be edited.";
}
}else if(s1 == "delete user 117 Manoj FC ManojPIN")
{
QString userid = "117";
QString message(tr("User Deleted Successfully"));
QSqlQuery query;
QString string = QString("delete from Users where UserID= "+ userid);
query.exec(string);
tcpSocket->write("UserID 117 Deleted Successfully");
qDebug()<<"User Deleted";
}
}
client.cpp
{
ui->setupUi(this);
tcpsocket = new QTcpSocket(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(requestNewFortune()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
this->setWindowTitle("Client");
}
void Client::requestNewFortune()
{
blockSize = 0;
tcpsocket->abort();
tcpsocket->connectToHost(ui->lineEdit->text(), ui->lineEdit_2->text().toInt());
connect(this->tcpsocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(this->tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(ui->AddUserBtn, SIGNAL(clicked()), SLOT(sendUserDetails()));
connect(ui->EditUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoEdit()));
connect(ui->DeleteUserBtn, SIGNAL(clicked()), SLOT(sendUserDetailstoDelete()));
connect(ui->TotalUserBtn, SIGNAL(clicked()), SLOT(GetTotalNumberofUsers()));
connect(ui->GetUserInfoBtn, SIGNAL(clicked()), SLOT(GetUserInfo()));
connect(ui->disconnect_Btn, SIGNAL(clicked()), SLOT(DisconnectFromServer()));
}
void Client::readFortune()
{
char cbuf[1024];
qint64 lineLength = tcpsocket->read(cbuf, sizeof(cbuf));
if (lineLength != -1) {
// the line is available in buf
}
ui->label_3->setText(cbuf);
}
void Client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
.....
}
}
void Client::sendUserDetails()
{
AddUser *idadduser = new AddUser(tcpsocket);
idadduser->exec();
}
void Client::GetTotalNumberofUsers()
{
char buf[12] = "TotalUsers";
qint64 lineLength = tcpsocket->write(buf, sizeof(buf));
}
void Client::GetUserInfo()
{
char buf[12] = "UserInfo";
tcpsocket->write(buf, sizeof(buf));
}
void Client::sendUserDetailstoEdit()
{
......
}
void Client::sendUserDetailstoDelete()
{
........
}
void Client::DisconnectFromServer()
{
tcpsocket->close();
}
To copy to clipboard, switch view to plain text mode
Bookmarks