Results 1 to 2 of 2

Thread: QTcpSocket and QDataStream

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QTcpSocket and QDataStream

    I'm learning a bit about TCP and QT by writing a server and client.

    I have noticed that the examples (Fortune Cookie server and client) use QDataStream to send and receive, and place a quint16 before the message indicating the expected size.

    I am curious why this approach was used. I have not seen any other servers I have looked at do anything similar (they seem to simply send the data without the quint16 at the start).

    I've been able to make it work both ways (with and without) but wonder which I should use?

    Is there some QT specific reason to use the quint16 method?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket and QDataStream

    Quote Originally Posted by December View Post
    I am curious why this approach was used.
    For simplicity.

    I have not seen any other servers I have looked at do anything similar (they seem to simply send the data without the quint16 at the start).
    Have you seen any xml-rpc based ones? Especially ones that use SOAP? Or ASN.1?

    As for other servers, they mostly use a text based protocol that separates messages with newline characters (like HTTP does for example)

    I've been able to make it work both ways (with and without) but wonder which I should use?
    Whichever you see fit.

    Is there some QT specific reason to use the quint16 method?
    QDataStream is a general purpose serialization mechanism, not a general purpose binary stream. It takes care of things like different endianness, string sizes, etc. Don't mix the two. QDataStream enables you to simply pipe your structures to the socket and expect them to be read correctly regardless of what platform listens at the other end as long as it uses QDataStream as well. QDataStream is mostly used for other things than sending data through network, but it works that way too.

Similar Threads

  1. QTcpSocket speed problem
    By benelgiac in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2007, 13:50
  2. How do I use QTcpSocket properly ?
    By mnemonic_fx in forum Qt Programming
    Replies: 13
    Last Post: 29th March 2007, 20:33
  3. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  4. Replies: 6
    Last Post: 8th January 2007, 10:24
  5. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08

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.