June 27, 2011

tchoninho tchoninho
Lab Rat
24 posts

How to I transfer file with size more than 65KB?

 

Hi,

I am trying to send a file, it has most size 65KB, but after receive it, it has only 65KB and the remainder of file is lost. Can someone help me?

Send code:

  1. sendFile(const QString filename, const QString version)
  2. {
  3.     if (! connected)
  4.        return;
  5.        
  6.         QFile fileSend(filename);
  7.         if (fileSend.open(QIODevice::ReadOnly))
  8.         {
  9.             QByteArray data(fileSend.readAll());
  10.  
  11.             Message msg;
  12.             msg.setTypes(Message::UpdatePlugin);
  13.             msg.setContents(QList<QVariant>()
  14.                             << filename << version);
  15.  
  16.             QByteArray block;
  17.             QDataStream out(&block, QIODevice::WriteOnly);
  18.             out.setVersion(QDataStream::Qt_4_6);
  19.  
  20.             out << (quint32)0;
  21.             out << msg;
  22.  
  23.             out.device()->seek(0);
  24.             out << (quint32)(block.size() - sizeof(quint32));
  25.  
  26.             tcpSocket->write(block);
  27.  
  28.             quint64 xx = 0;
  29.             while (xx < data.size())
  30.                xx += tcpSocket->write(data);
  31.  
  32.            qWarning("data.size(): %u,  xx: %u \n", data.size(), xx);
  33.  
  34.             // tcpSocket->write(block);
  35.             tcpSocket->flush();
  36.         }
  37. }

Receive code:

  1. receiveFile(Message msg)
  2. {
  3.     QList<QVariant> contents = msg.getContents();
  4.     QString filename(contents.at(0).toString());
  5.     QFile fileReceived(filename);
  6.     if (fileReceived.open(QIODevice::Append))
  7.     {
  8.  
  9.         quint64 x = 0;
  10.         QByteArray data(readAll());
  11.  
  12.         qWarning("data.size(): %u \n", data.size());
  13.  
  14.         while (x < data.size())
  15.             x += fileReceived.write(data);
  16.  
  17. }

 Signature 

Computer Scientist
Belo Horizonte - Brasil
C++ Development
Qt Development

6 replies

June 27, 2011

koahnig koahnig
Gene Splicer
2905 posts

you might need a while loop in your receive part. At least that part is missing in your source snippet for receiving the information.

June 27, 2011

Jupiter Jupiter
Lab Rat
95 posts

isnt 65kb the internal QIODevice buffersize? so koahnig might be right, you have to loop over it with in a readyRead slot with

  1. read(bytesAvailable())

June 27, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

The best way here is to either send total data size (so receiver will know how much it should receive) or send some boundary sequence (as multipart/form-data http content-type does)

June 27, 2011

Andre Andre
Robot Herder
6399 posts

Denis is right. I highly prefer the first solution. Saves you from escaping and related headaches, and allows for things like progress bars on the receiving end. Just send a quint64 that indicates the amount of data you will be sending before actually starting the send.

June 27, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

As addition to Andre. You of course need to add some timeouts on reading in case if network connection will be broken, etc. Or you will wait forever for this amount stored in quint64 :)

June 28, 2011

koahnig koahnig
Gene Splicer
2905 posts

Well, if you start the discussion on how to do best. The question is also, do you really want to send just a big file? For a couple of 100 kB that is acceptable. For a couple of MB it might make sense to spend a little more thinking. Memory is not as expensive anymore as it was, but ultimately one is always reaching the boundaries.
So cutting the file into chunks on both is certainly a good thing to think about.

 
  ‹‹ [SOLVED] [N00b] Memory management in Qt      QList<QPixmap> ’index out of range’ ››

You must log in to post a reply. Not a member yet? Register here!