PDA

View Full Version : How can I be sure that all data has arrived? Network newbie


Morea
17th June 2007, 21:02
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?

marcel
17th June 2007, 21:08
No. QTcpSocket is state oriented, so you must examine it's states while you send data( QAbstractSocket::SocketState (http://www.qtcentre.org/forum/qabstractsocket.html#SocketState-enum))

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

jacek
17th June 2007, 21:13
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.

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().

wysota
17th June 2007, 21:23
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.