Results 1 to 6 of 6

Thread: Send exe file over QTcpSocket

  1. #1
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Send exe file over QTcpSocket

    Hello,

    I am trying to send an EXE file over QTcpSocket, but it comes back with the wrong data.
    I have researched and think it is because it only is getting the first "packet"?
    I'm new to Tcp sockets should I use QDataStream?


    Server side:
    Qt Code:
    1. ...
    2. QByteArray byteArray;
    3. QFile file("test.exe");
    4. if(file.open(QIODevice::ReadOnly)) {
    5. byteArray = file.readAll();
    6. }
    7. file.close();
    8. socket->write(byteArray);
    9. ...
    To copy to clipboard, switch view to plain text mode 

    Client side:
    Qt Code:
    1. ...
    2. int bytesReceived = 0;
    3. readyRead(){
    4. QByteArray byteArray;
    5. if (tcpSocket->waitForReadyRead()) {
    6. bytesReceived += (int)tcpSocket->bytesAvailable();
    7. byteArray += tcpSocket->readAll();
    8. qDebug() << byteArray.size();
    9. qDebug() << "recieved: " + QString::number(bytesReceived);
    10.  
    11. updateFile = new QFile("test.exe");
    12. if(updateFile->open(QIODevice::WriteOnly)) {
    13. qDebug() << updateFile->write(byteArray);
    14. }
    15. }
    16. ...
    To copy to clipboard, switch view to plain text mode 


    Output:
    Qt Code:
    1. 87600
    2. "recieved: 87600"
    3. 87600
    4. 131072
    5. "recieved: 218672"
    6. 131072
    7. 131072
    8. "recieved: 349744"
    9. 131072
    10. 131072
    11. "recieved: 480816"
    12. 131072
    13. 131072
    14. "recieved: 611888"
    15. 131072
    16. 131072
    17. "recieved: 742960"
    18. 131072
    19. 131072
    20. "recieved: 874032"
    21. 131072
    22. 131072
    23. "recieved: 1005104"
    24. 131072
    25. 131072
    26. "recieved: 1136176"
    27. 131072
    28. 129488
    29. "recieved: 1265664"
    30. 129488
    To copy to clipboard, switch view to plain text mode 

    test.exe in written to file but is 129,488 bytes when it should be 1265664.
    So server writes the correct data to client but client interprets it wrong?

    Thank you for any help

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Send exe file over QTcpSocket

    You only wait for the first ready read so you obviously only get the data that has been recieved until that point.

    If the sender closes the socket after sending you could try waiting for disconnected or waiting for readyRead in a loop until it the socket is closed

    Cheers,
    _

  3. #3
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Send exe file over QTcpSocket

    Quote Originally Posted by anda_skoa View Post
    You only wait for the first ready read so you obviously only get the data that has been recieved until that point.

    If the sender closes the socket after sending you could try waiting for disconnected or waiting for readyRead in a loop until it the socket is closed

    Cheers,
    _
    Hi anda_skoa,

    I don't understand what you mean.
    The sender doesn't close any connections.

    Thanks

  4. #4
    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: Send exe file over QTcpSocket

    Quote Originally Posted by scarecr0w132 View Post
    The sender doesn't close any connections.
    It's about the receiver, not the sender. You're expecting all data to magically be teleported to the receiving machine and only then the receiving application be notified about the data. That's not the case, if you have N bytes of data then it takes some time to send it, it takes some time for it to be transported and it takes some time to be received. The receiving application is notified about the incoming data not when the N'th byte arrives but rather when the first byte arrives. Thus if you "read all" then you only read the part that has already been received (usually up to one TCP receiving window, which seems to be 128kB on your system).
    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.


  5. The following user says thank you to wysota for this useful post:

    scarecr0w132 (20th October 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Send exe file over QTcpSocket

    Quote Originally Posted by scarecr0w132 View Post
    I don't understand what you mean.
    The sender doesn't close any connections.
    Well, in that case you will need a different way for the receiver to know that all data has been received.
    For example by sending the number of bytes first.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    scarecr0w132 (20th October 2013)

  8. #6
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Send exe file over QTcpSocket

    Fixed
    thankyou

Similar Threads

  1. Cannot send data with QTcpSocket
    By stef13013 in forum Qt Programming
    Replies: 6
    Last Post: 1st September 2012, 12:00
  2. how to use qtcpsocket send qimage data
    By tsuibin in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2012, 14:51
  3. how to send data using QTcpSocket and QTcpServer
    By gauravg in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2012, 12:29
  4. QTcpSocket does not send data immediately
    By igorosberg in forum Qt Programming
    Replies: 11
    Last Post: 15th July 2011, 15:51
  5. How to send Pixmap through QTcpSocket?
    By live_07 in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2008, 16:35

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.