Help with a simple client programe
Hi all,
I'm coding a simple client-server communication programe, wherein I require to send a struct data to server. Is it possible to send the struct as a single packet to the server??
if yes, then how to do it?
any help will be apprecated.
regards
Re: Help with a simple client programe
Hi,
a good starting point for this is Qt Network Examples.
For more detailed information have look at Qt Network Module.
For serialization of data structures have a look at QDataStream.
For further help - just ask!
Good luck
Tom
Re: Help with a simple client programe
will not that serialization of the structure data make it different packets, if I'll use QDataStream? , b'coz all the server is looking for is a single packet, which consists of certain header along with data like username.
kindly throw some light on it pls.
Thanks
Re: Help with a simple client programe
What do you mean by "single packet"?
QDataStream will help you to keep your code base small and provides an easy-to-use way to handle serialization.
QDataStream will help you to avoid nasty parsing and/or byte counting stuff.
Re: Help with a simple client programe
by 'single packet' I meant to say that, the packet structure consist of header, a key, and the data -- which is username. Now when we're going to use QDataStream, we'll be using "<<" operator to send each and every variable of the struct. so my query is will it send differently of just as serially as it is there, in the QDataStream, so that the server won't receive the struct in different format altogether at its end.
Thanks :)
Re: Help with a simple client programe
Quote:
Originally Posted by
QuTe
... Now when we're going to use QDataStream, we'll be using "<<" operator to send each and every variable of the struct.
...
You can stream a whole struct/class to QDataStream by defining stream operators for the struct/class.
This is a little bit nicer than every time streaming the struct members.
Good luck,
Tom
Here some pseudo-code to show what I mean:
Code:
class SomeClass
{
...
private:
int _mSomeInt;
};
Code:
{
return out << (quint32)(c._mSomeInt) << c._mSomeString;
}
{
quint32 i;
in >> i;
c._mSomeInt = i;
in >> c._mSomeString;
return in;
}
Re: Help with a simple client programe
Hi Tom
Thanks for the helpful suggestions. But when I used the my own operator "<<" definition, its not detecting it somehow! i don't know whats wrong, but its throwing the error that "no match ' ' found", for the operator I'm using.
I've declared the QDatastream operator the same way you've shown in the snippet above. but it still doesn't work for me.
pls help.
Thanks
Re: Help with a simple client programe
Attach the code and we can have a look!
Tom
Re: Help with a simple client programe
ok here you go then:
header file:
Code:
#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
and the cpp file:
Code:
#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;
}
after compilation the compiler throw the error::
error: no match for 'operator <<' in 'sendData <<Client::clientPkt'
Re: Help with a simple client programe
oops my bad!
I just realised what I did :o, I should have implemented the 'operator<<' before utilising it in the program. I mistakenly put it later.
nevermind sry about that.
Thanks alot for the help Tom :)
Regards
Re: Help with a simple client programe
Now, everything is working fine, except the fact that, my server is not being able to recognize the protocol which we use( we put the a string to recognize the packets in its header). and I checked with the server data and I found that, actually the server is getting '0' s ( zeros) in between each of the character data I'm sending!! and btw I'm sending it using QDataStream.
And I also tried with QByteArray, and well it works bit fine with it, as compared to QDataStream, but in QByteArray as well, I'm facing a problem, which is, my server doesn't get the size of the packet( which is one of the members of the packet structure which I'm sending from here)
Now is there any other simple way to send data packets to server without getting the data manipulated while sending using Qt APIs???
pls help
Regards
Re: Help with a simple client programe
Quote:
Originally Posted by
QuTe
I checked with the server data and I found that, actually the server is getting '0' s ( zeros) in between each of the character data I'm sending!!
By default Qt sends strings in UTF-16 --- that's why each character is encoded using two bytes.
Quote:
Originally Posted by
QuTe
and btw I'm sending it using QDataStream.
QDataStream uses its own protocol for serialization, see here.
Quote:
Originally Posted by
QuTe
Now is there any other simple way to send data packets to server without getting the data manipulated while sending using Qt APIs???
Use QDataStream::writeRawData(), QDataStream::writeBytes() or QIODevice::write().