Results 1 to 5 of 5

Thread: Serialized QByteArray length

  1. #1
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Serialized QByteArray length

    I want to send serialized QByteArray (may have arbitrary size) via TCP socket. I have read that signal readyRead() can be emitted even not all data available and user must manually check it by calling bytesAvailable(). But I didn't found how to get array's size on receiver side.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serialized QByteArray length

    Once way would be to send the array size first, and then send the array. That way you know the size of the array on the receiver side.

  3. #3
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialized QByteArray length

    What is minimal size when readyRead signal emitted? Is it guaranteed more than 4 bytes?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Serialized QByteArray length

    It could be 1 byte.

    If your array will be less than 255 characters then send 1 byte for the length.
    If your array will be less than 65535 characters then send 2 bytes for the length and ensure you have 2 bytes before assuming you have the length.
    etc.

  5. The following user says thank you to squidge for this useful post:

    giantdragon (21st May 2011)

  6. #5
    Join Date
    Jan 2011
    Posts
    34
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serialized QByteArray length

    Quote Originally Posted by squidge View Post
    It could be 1 byte.

    If your array will be less than 255 characters then send 1 byte for the length.
    If your array will be less than 65535 characters then send 2 bytes for the length and ensure you have 2 bytes before assuming you have the length.
    etc.
    Thanks.
    I think it will be better to send always 32-bit interger (qint32) and check bytesAvailable >= 4.

Similar Threads

  1. length of Plotmarker lines
    By FelixB in forum Qwt
    Replies: 2
    Last Post: 12th October 2010, 12:57
  2. Is it possible to set a maximum length for QTextEdit ???
    By vairamuthu.g in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2010, 05:17
  3. Set Maximum Length of QTextEdit
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2010, 08:27
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. qt4 & xp - QTextEdit length
    By incapacitant in forum Newbie
    Replies: 4
    Last Post: 6th March 2006, 15:34

Tags for this Thread

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.