#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextDocument>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QTextStream>
#include <qglobal.h>
#include <QDataStream>
struct clientPktStruct {
qint32 size_of_pkt;
qint8 key;
};
{
Q_OBJECT
public:
~Client()
{
}
private slots:
void closeConnection();
void sendToServer();
void socketReadyRead();
void socketConnected();
void socketConnectionClosed();
void socketClosed();
private:
struct clientPktStruct clientPkt;
quint16 port;
quint16 blockSize;
};
#endif
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include <QTcpSocket>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextDocument>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QTextStream>
#include <qglobal.h>
#include <QDataStream>
struct clientPktStruct {
QString pktString;
qint32 size_of_pkt;
qint8 key;
QString uName;
};
class Client : public QWidget
{
Q_OBJECT
public:
Client(QWidget *parent=0);
~Client()
{
}
private slots:
void closeConnection();
void sendToServer();
void socketReadyRead();
void socketConnected();
void socketConnectionClosed();
void socketClosed();
void displayError( QAbstractSocket::SocketError );
private:
QPushButton *okay;
QPushButton *cancel;
QString strnglist;
QString newChallengeString;
QTcpSocket *socket;
QFile *infoText;
QDataStream Debug;
QLabel *userNameLabel;
QLabel *userPasswdLabel;
QLineEdit *userNameText;
QLineEdit *userPasswdText;
struct clientPktStruct clientPkt;
quint16 port;
quint16 blockSize;
friend Q_CORE_EXPORT QDataStream &operator<< (QDataStream &, const struct clientPktStruct &);
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include <QLineEdit>
#include <QBoxLayout>
#include <QHostAddress>
#include "clientSocket.h"
{
// GUI layout
userNameLabel
= new QLabel(tr
("UserName:"),
this);
userPasswdLabel
= new QLabel(tr
("Password:"),
this);
userPasswdText
->setEchoMode
(QLineEdit::Password);
labelLayout->addWidget(userNameLabel, 0, 0);
labelLayout->addWidget(userNameText, 0, 1);
labelLayout->addWidget(userPasswdLabel, 1, 0);
labelLayout->addWidget(userPasswdText, 1, 1);
hb->addWidget(okay);
hb->addWidget(cancel);
mainLayout->addLayout(labelLayout);
mainLayout->addStretch(1);
mainLayout->addLayout(hb);
setLayout(mainLayout);
connect( okay, SIGNAL(clicked()), SLOT(sendToServer()) );
connect( cancel, SIGNAL(clicked()), qApp, SLOT(quit()) );
// create the socket and connect various of its signals
host.setAddress("203.187.156.70");
port = 5000;
connect( socket, SIGNAL(connected()),
SLOT(socketConnected()) );
connect( socket, SIGNAL(connectionClosed()),
SLOT(socketConnectionClosed()) );
connect( socket, SIGNAL(readyRead()),
SLOT(socketReadyRead()) );
// connect to the server
clientPkt.pktString = "ISHR1.00";
clientPkt.key = 101;
socket->connectToHost( host, port );
}
........
........
void Client::sendToServer()
{
// write to the server
clientPkt.uName = userNameText->text();
clientPkt.size_of_pkt = sizeof(clientPkt);
/* this is where the operator will be used */
sendData<<clientPkt;
userNameText->setText( "" );
okay->setEnabled(false);
}
..........
..........
.........
/* operator for datastreaming a structure field */
{
return sendPkt <<CPkt.pktString<<CPkt.size_of_pkt<<CPkt.key<<CPkt.uName;
}
#include <QtGui>
#include <QLineEdit>
#include <QBoxLayout>
#include <QHostAddress>
#include "clientSocket.h"
Client::Client(QWidget *parent)
:QWidget(parent)
{
// GUI layout
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QHBoxLayout *hb = new QHBoxLayout;
QGridLayout *labelLayout = new QGridLayout;
userNameLabel = new QLabel(tr("UserName:"), this);
userPasswdLabel = new QLabel(tr("Password:"), this);
userNameText = new QLineEdit(this);
userPasswdText = new QLineEdit(this);
userPasswdText->setEchoMode(QLineEdit::Password);
labelLayout->addWidget(userNameLabel, 0, 0);
labelLayout->addWidget(userNameText, 0, 1);
labelLayout->addWidget(userPasswdLabel, 1, 0);
labelLayout->addWidget(userPasswdText, 1, 1);
okay = new QPushButton( tr("ok") , this );
cancel = new QPushButton( tr("cancel") , this );
hb->addWidget(okay);
hb->addWidget(cancel);
mainLayout->addLayout(labelLayout);
mainLayout->addStretch(1);
mainLayout->addLayout(hb);
setLayout(mainLayout);
connect( okay, SIGNAL(clicked()), SLOT(sendToServer()) );
connect( cancel, SIGNAL(clicked()), qApp, SLOT(quit()) );
// create the socket and connect various of its signals
QHostAddress host;
host.setAddress("203.187.156.70");
port = 5000;
socket = new QTcpSocket( this );
connect( socket, SIGNAL(connected()),
SLOT(socketConnected()) );
connect( socket, SIGNAL(connectionClosed()),
SLOT(socketConnectionClosed()) );
connect( socket, SIGNAL(readyRead()),
SLOT(socketReadyRead()) );
connect( socket, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(displayError(QAbstractSocket::SocketError)) );
// connect to the server
clientPkt.pktString = "ISHR1.00";
clientPkt.key = 101;
socket->connectToHost( host, port );
}
........
........
void Client::sendToServer()
{
// write to the server
clientPkt.uName = userNameText->text();
clientPkt.size_of_pkt = sizeof(clientPkt);
QDataStream sendData(socket);
sendData.setVersion(QDataStream::Qt_4_0);
/* this is where the operator will be used */
sendData<<clientPkt;
userNameText->setText( "" );
okay->setEnabled(false);
}
..........
..........
.........
/* operator for datastreaming a structure field */
QDataStream &operator<< (QDataStream &sendPkt, const struct clientPktStruct &CPkt)
{
return sendPkt <<CPkt.pktString<<CPkt.size_of_pkt<<CPkt.key<<CPkt.uName;
}
To copy to clipboard, switch view to plain text mode
Bookmarks