开发者

QTcpServer- QTcpSocket acknowledgement question

-->I am writing a program using QTcpSocket,QTcpServer. Do I need to add some code(i.e. an acknowledgement procedure) to guarantee that the packets are delivered?

If this is the case, I could not find anything like that on the net, therefore would like to see some examples,please.

-->Or in the QT library somewhere, these are already covered so that I do not need anything like that?

Any help will be appreciated. Thanks.

EDIT: Now, I am asking how to use headers with the chunks of QDataStream I added below. I want each and every chunk to have headers telling information.So that I can keep track of which parts are written, which are missing.. Please have a look at below.

///RECEIVER SIDE

QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);

QByteArray next;
next=socket->readAll(); 

QF开发者_如何学Pythonile 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();


TCP guarantees ordered delivery. Once you submit data to TCP socket the stack will try to send as much as it can. Stack uses internal acknowledgement messages to guarantee delivery.

So you don't need to send acknowledgements however, it is a good idea to implement a header for your packets so that receiving side can properly merge small packets back into a huge file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜