Results 1 to 3 of 3

Thread: QTcp[server and socket]: can't read my transfered file, why!!

  1. #1
    Join Date
    Sep 2012
    Posts
    9
    Qt products
    Platforms
    Windows

    Angry QTcp[server and socket]: can't read my transfered file, why!!

    Good morning,
    I tried to create my own code. I have an application, in which, the user will choose a file from his DD ( all types) and send it to the TcpServer, this server will then send this file to the other clients.But, I have a problem, when i choose the file and i send it, in the client’s side, i have this message: file is sending , but in the server’s side, it shows me that the file isn’t recieved with it’s totaly bytes. Any suggestion please. This is the function for sending the file in the client’s side:
    Qt Code:
    1. void FenClient::on_boutonEnvoyer_2_clicked()
    2. {
    3. QString nomFichier = lineEdit->text();
    4. QFile file(lineEdit->text());
    5. if(!file.open(QIODevice::ReadOnly))
    6. {
    7. qDebug() << "Error, file can't be opened successfully !";
    8. return;
    9.  
    10. }
    11.  
    12. QByteArray bytes = file.readAll();
    13.  
    14. QByteArray block;
    15. QDataStream out(&block, QIODevice::WriteOnly);
    16.  
    17. out << quint32(0);
    18. out << nomFichier;
    19. out << bytes;
    20. out.device()->seek(0);
    21. out << quint32((block.size() - sizeof(quint32)));
    22.  
    23. qDebug() << "Etat : envoi en cours...";
    24. listeMessages->append("status : sending the file...");
    25.  
    26. socket->write(block);
    27. }
    To copy to clipboard, switch view to plain text mode 
    and the server side:
    Qt Code:
    1. void FenServeur::datarecieved()
    2. {
    3.  
    4. QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    5.  
    6. if(socket == 0)
    7. {
    8. qDebug() << "no Socket!";
    9. return;
    10. }
    11.  
    12. forever
    13. {
    14. QDataStream in(socket);
    15. if(blockSize == 0)
    16. {
    17. if(socket->bytesAvailable() )
    18. {
    19. qDebug() << "Error < sizeof(quint32))";
    20. return;
    21. }
    22.  
    23. in >> blockSize;
    24. }
    25.  
    26. if(socket->bytesAvailable() < blockSize)
    27. {
    28. qDebug() << "data not recieved with its total bytes";
    29.  
    30. return;
    31. }
    32.  
    33. qDebug() << "!!!!!!";
    34. QByteArray dataOut;
    35. QString nameFile;
    36. in >> nameFile >> dataOut;
    37. QFile fileOut(nameFile);
    38. fileOut.open(QIODevice::WriteOnly);
    39. fileOut.write(dataOut);
    40. fileOut.close();
    41.  
    42. blockSize = 0;
    43. }
    44. }
    45.  
    46. void FenServeur::sendToAll(const QString &message)
    47. {
    48.  
    49. QByteArray paquet;
    50. QDataStream out(&paquet, QIODevice::WriteOnly);
    51.  
    52. out << (quint32) 0;
    53. out << message;
    54. out.device()->seek(0);
    55. out << (quint32) (paquet.size() - sizeof(quint32));
    56. for (int i = 0; i < clients.size(); i++)
    57. {
    58. clients[i]->write(paquet);
    59. }
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 
    So i can't write the file that the server recieved into a new file, or i have to write it in a filejust in the client side!!!. Any suggestion please!! and thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcp[server and socket]: can't read my transfered file, why!!

    Do you think posting the same problem in two threads is going to give you twice as many solutions to your problem?

    http://www.qtcentre.org/threads/5135...read-file-sent
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2012
    Posts
    9
    Qt products
    Platforms
    Windows

    Default Re: QTcp[server and socket]: can't read my transfered file, why!!

    certainly no , i tried to delete the other post, but i couldn't do it , sorry

Similar Threads

  1. QTCP Socket Server and threads
    By cafu1007 in forum Qt Programming
    Replies: 21
    Last Post: 2nd November 2012, 17:00
  2. Replies: 6
    Last Post: 7th October 2012, 17:42
  3. Replies: 2
    Last Post: 22nd May 2011, 22:31
  4. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 13:46
  5. non GUI socket server
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2008, 12:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.