PDA

View Full Version : tcp custom data "header"



Talei
29th April 2012, 01:31
Hello,
I have question regarding implementation of some kind header to the send tcp data.

Basically what I want to achieve is to create some kind of header (probably few data in first packet) when sending data over QTcpSocket with additional fields like data size, etc.

And my question:
what is best way to go about it?

My firs idea is to use some kind of divider so I could write message like this:


QByteArray sendData;
QByteArray msgHeader;
msgHeader.append( "1" );
msgHeader.append( "|" ); // custom divider for fields in header
msgHeader.append( "125" );
msgHeader.append( "|" ); // custom divider for fields in header

QByteArray packet;
packet.append( msgHeader );
packet.append( sendData );

QTcpSocket().write( packet );
so message would looks like, i.e., this:


1|125|binaryDataHere

and on other end I would parse header data.
Is it good idea?

ChrisW67
29th April 2012, 08:41
You can do it however you want: your protocol, your rules. However, if the data is truly binary then it is possibly going to contain the pipe symbol (or any other delimiter you choose) so you need to keep that in mind.

Talei
29th April 2012, 14:43
That's why i ask here for opinion.
My initial thought is to just read until i have i.e. 3 pipes (they will be set in fixed numbers depending on "header" size.

Thank you for reply.