Results 1 to 3 of 3

Thread: tcp custom data "header"

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default tcp custom data "header"

    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:

    Qt Code:
    1. QByteArray sendData;
    2. QByteArray msgHeader;
    3. msgHeader.append( "1" );
    4. msgHeader.append( "|" ); // custom divider for fields in header
    5. msgHeader.append( "125" );
    6. msgHeader.append( "|" ); // custom divider for fields in header
    7.  
    8. QByteArray packet;
    9. packet.append( msgHeader );
    10. packet.append( sendData );
    11.  
    12. QTcpSocket().write( packet );
    To copy to clipboard, switch view to plain text mode 
    so message would looks like, i.e., this:

    1|125|binaryDataHere
    and on other end I would parse header data.
    Is it good idea?
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: tcp custom data "header"

    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.

  3. The following user says thank you to ChrisW67 for this useful post:

    Talei (29th April 2012)

  4. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: tcp custom data "header"

    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.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

Similar Threads

  1. Replies: 4
    Last Post: 18th April 2010, 00:37
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 2
    Last Post: 28th February 2010, 07:38
  4. Replies: 3
    Last Post: 25th August 2009, 13:03
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.