Results 1 to 4 of 4

Thread: saving data to server through QTcpSocket

  1. #1
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default saving data to server through QTcpSocket

    Qt Code:
    1. void myClient::readyRead()
    2. {
    3. //qDebug() << socket->readAll();
    4. QString fPath("location");
    5.  
    6. QByteArray block=socket->readAll();
    7. QString fName= QString(block);
    8. QFile sentFile(fPath +QDir::separator() + fName);
    9.  
    10. if(!sentFile.exists())
    11. {
    12. sentFile.open(QFile::WriteOnly);//check if size equal to size or overwrite
    13. while(socket->waitForReadyRead())
    14. {
    15. QByteArray block= socket->readAll();
    16. qDebug()<< "read: " << block.size();
    17. sentFile.write(block);
    18. block.clear();
    19. }
    20. socket->close();
    21. sentFile.close();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    i guess the while loop is blocking my GUI
    how can l make this code non-Blocking
    Thanks and best regards.
    Last edited by wysota; 6th January 2013 at 10:36. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: saving data to server through QTcpSocket

    You do it by opening the receiving file before the transfer, writing all available bytes into that file on each call (of many) your readyRead() handler receives, and closing the file when the finished signal is received. If the files are guaranteed to be small, i.e. not exhaust memory, then you can simply wait for the finished() signal, check for errors, and read the entire file in one hit. No loops required.

    You are also making the serious mistake of assuming that the file name arrives in one block that is exactly the size of the string you were expecting. This will almost never be the case and certainly cannot be relied on. You need a smarter protocol.

  3. The following user says thank you to ChrisW67 for this useful post:

    toufic.dbouk (6th January 2013)

  4. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: saving data to server through QTcpSocket

    ohh i see what u mean , thanks.
    can you tell me of a way to send the file name as one block followed by another block for the file size
    such that these two block will always be in just 2 blocks so that i can receive them knowing the first is the file name and the second is the file size ?
    can i add a separator after each like (*^) and then split at the receivers side ?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: saving data to server through QTcpSocket

    can you tell me of a way to send the file name as one block followed by another block for the file size
    such that these two block will always be in just 2 blocks so that i can receive them knowing the first is the file name and the second is the file size ?
    No, I cannot because that is not how the network works. The only guarantee that TCP provides is that bytes transmitted will be received in the correct order: under the covers it is ensuring order, splitting/joining blocks for network hops that can only handle small/certain packet sizes, and requesting retransmission of blocks that have been corrupted or lost (both reasonably frequent events).

    You need to send sufficient information, or define your protocol in such a way, that the receiver can determine/know the number of bytes in the file name. Typically this would be done by:
    • First sending 1, 2 or 4 bytes (watch byte order issues) indicating the number of bytes in the following file name, then sending the file name, then the file content. The receiver has several states: wait for size bytes, wait for file name, and read data.
    • Defining a header that has either a known length or a known pattern of terminating bytes so you can detect the end of header/start of data.

    There is more ways to do this: FTP uses a separate socket to send control information like file names, HTTP uses a Content-Disposition header to send the file name on a set of headers terminated by two CR LF pairs.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. QTcpSocket/QTcpServer on a heavy load server
    By Eumcoz in forum Qt Programming
    Replies: 10
    Last Post: 7th August 2012, 19:36
  2. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 08:45
  3. Replies: 0
    Last Post: 5th February 2011, 08:31
  4. Replies: 8
    Last Post: 18th December 2010, 15:07
  5. My server (using QTcpServer and QTcpSocket) crashes
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2010, 15:32

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.