-->我正在用QTcpSocket,QTcpServer写一个程序。我是否需要添加一些代码(即确认过程)来保证数据包已送达?
If this is the case, I could not find anything like that on the net, therefore would like to see some examples,please.-->或者在QT库中的某个地方,这些已经被覆盖,所以我不需要这样的东西?
任何帮助都将不胜感激。谢谢。
编辑:现在,我正在询问如何将标题与我在下面添加的QDataStream块一起使用。我希望每个块都有标头,告诉information.So我可以跟踪哪些部分已写入,哪些部分缺失。请看下面的内容。
///RECEIVER SIDE
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
QByteArray next;
next=socket->readAll();
QFile output("c:\\some\\output.txt");
output.open(QIODevice::Append);
output.write(next);
output.close();
///SENDER SIDE
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
QFile file(path);
if(file.exists()){
file.open(QIODevice::ReadOnly);
}
QByteArray data;
data=file.readAll();
block.append(data);
out << (quint64)0;
out.device()->seek(0);
if(!socket->waitForBytesWritten()){
qDebug()<<"writing";
socket->write(block);
socket->flush();
}
file.close();
qDebug()<<"data: size"<<file.size();
socket->disconnectFromHost();发布于 2011-07-07 15:26:47
TCP保证有序的传送。一旦您将数据提交到TCP套接字,堆栈将尝试发送尽可能多的数据。堆栈使用内部确认消息来保证传递。
所以你不需要发送确认,但是,最好为你的包实现一个头,这样接收端就可以正确地将小包合并回一个大文件中。
https://stackoverflow.com/questions/6601379
复制相似问题