Results 1 to 4 of 4

Thread: How can I be sure that all data has arrived? Network newbie

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How can I be sure that all data has arrived? Network newbie

    I'm a network programming newbie so I must ask this silly question.

    If I send data over the network with a QTextStream connected to a socket, must I add newline to the end of the data I wish to send?

    How can I be sure that I've got all the data?
    Should I write a slot that is connected to readyRead and when it's called, it checks canReadLine(), and if true, it then read the line?

    Might this be the reason for sending a \n to indicate that the line is finished?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I be sure that all data has arrived? Network newbie

    No. QTcpSocket is state oriented, so you must examine it's states while you send data( QAbstractSocket::SocketState)

    QTcpSocket is also a subclass of QIODevice.So you must read about this too.

    I suggest looking at the examples in the Networking section in the Examples and Demos.
    You should get started there.
    But first read the docs for QAbstractSocket.

    Regards
    Last edited by wysota; 17th June 2007 at 20:17. Reason: reformatted to look better

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I be sure that all data has arrived? Network newbie

    Quote Originally Posted by Morea View Post
    If I send data over the network with a QTextStream connected to a socket, must I add newline to the end of the data I wish to send?
    No, you don't have to add it.

    Quote Originally Posted by Morea View Post
    Might this be the reason for sending a \n to indicate that the line is finished?
    Yes, if the receiver doesn't know the length of the text you want to send, it won't be able to tell whether it received all of the data, unless you send some kind of marker after the data.

    See also QTextStream::flush().

  4. #4
    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: How can I be sure that all data has arrived? Network newbie

    TCP sends a stream of data, so technically speaking the only way to know the whole data is received is when the sending side of the connection sending the data is gracefully shutdown (the stream is finished). Using newlines or any other markers, as mentioned by Jacek, is a way to emulate datagrams in a non-datagram protocol.

    Newlines have a side effect - they cause streams to be flushed and in case of TCP the flush is done by adding a PUSH flag to the stream which forces the TCP stack to transmit the data without waiting for the whole transmitting window to be assembled or to pass the data immediately to the application on the receiving side. But using newlines is not required, the stack will send the data anyway, it might just wait a second or so before doing that.

Similar Threads

  1. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53
  2. data at fifo's receiving end has arrived?
    By quickNitin in forum General Programming
    Replies: 4
    Last Post: 5th November 2006, 10:05
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

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.