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)

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 << quint16(0) << hello << version << clientType << length << identifier;
out << quint16(block.size()- sizeof(quint16));
ui->currentStatusLabel->setText(tr("Sending request..."));

In the internet I saw examples using struct and pack in python. don't know how to use it in C++.

Thanks in advance.

out << quint16(0) << hello << version << clientType << length << identifier;

First of all, you are violating the protocol. As described, it requires that the packet starts with the length of the whole packet as a 4 byte value.

Second, Version and ClientType are one byte each; you define (and send) them as shorts, which is most likely 2 bytes.

It is more or less OK to send "hello" as quint16, provided that both server and client agree on the endianness. I know nothing about your server, and may only assume that it uses network byte order. In that case you'd have to find out how to set the QDataStream into network byte order (it accidentally is the same as big endianness).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.