PDA

View Full Version : Qt-- Client Server communication problem



thisismyuname
7th January 2010, 21:48
Hallo,

I must develop a Client application using (Qt) C++. The server application was developed by somebody. I need to register with the Server application to get data from it. I have problems here. Can anybody please let me know how to proceed.

In order to register with the server, the client must first send a Hello instruction to it. And the format for Hello is as follows:
Length Contents Term Description
4 byte xx xx xx xx PACKET_LENGTH Length of the packet in byte
2 byte 00 01 HELLO Command Hello
1 byte 02 VERSION Version of protocol
1 byte 02 Client Type
variable xx IDENTIFICATION String to identify program

For eg., I register with the Server with following command
00 01 02 02 09 MyProgram
where 00 01 is for hello
02 is for version
02 is for Client type
08 is for length of my string (MyProgram)

Question:
How can I send the above command to Server?? What kind of data type should i use for storing this command?? Please note the data must be sent in Motorola byte order.

I tried with the following code but the server is not accepting.

void ClientDialog::sendRequest()
{

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
qint16 hello = 0001; // I think problem is here.
short version = 02;
short clientType = 02;
QString identifier = "MyProgram";
qint8 length = identifier.size();

out.setVersion(QDataStream::Qt_4_5);
//QDataStream::BigEndian;
out << quint16(0) << hello << version << clientType << length << identifier;
out.device()->seek(0);
out << quint16(block.size()- sizeof(quint16));
tcpSocket.write(block);
ui->currentStatusLabel->setText(tr("Sending request..."));
}

Thanks in advance.

wysota
7th January 2010, 22:10
QDataStream is of no use to you. You need to use QByteArray and then write the array directly to the socket.

Tanuki-no Torigava
8th January 2010, 01:04
And swap bits on send and recieve