[Qt-interest] Transferring a QPixmap through QTcpSocket

Thiago Macieira thiago at kde.org
Thu Apr 7 15:28:02 CEST 2011


On Thursday, 7 de April de 2011 17:08:46 Mandeep Sandhu wrote:
> > The client receives some data, but how to I:
> >     1. Ensure that all of the sent data is received, and
> 
> Doesn't a TCP connection ensure that, i.e no packets are lost (unlike
> UDP)? So once you've read all data off the socket it should be the
> complete byte stream sent by your server. CMIIW.

That is correct.

But note you said "once you've read all the data". So the question is still: 
how do you know that you've read it all and that no bytes remain, still to be 
received by the network?

There are usually two ways of doing this (from your "programming 101" class):

 - send the length in advance
 - send a sentinel afterwards

C strings use a sentinel afterwards (the NUL). You can't send a specific byte 
sequence since the data you're sending is arbitrary and binary. So the only 
sentinel you can send is the "connection close".

Given that the OP was closing the socket just after writing the pixmap data, 
then sending the length in advance is unnecessary. Conclusion:

Sender:
    connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
    socket->connectToHost(...);
    socket->write(block);
    socket->disconnectFromHost();
    socket = 0;

Receiver:
    socket = server->nextPendingConnection();
    connect(socket, SIGNAL(disconnected()), this, SLOT(readAllData()));

void readAllData()
{
    QByteArray data = socket->readAll();
    delete socket;
}
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110407/a215d729/attachment.bin 


More information about the Qt-interest-old mailing list