Results 1 to 8 of 8

Thread: Continously receive from server using waitforReadyread()

  1. #1
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Continously receive from server using waitforReadyread()

    I am new in QT i have written one server client application in where my server will continuously send some file names to client and client should receive them correspondly one by one.In client side to receive the file names i am using "waitforReadyread()" in a while loop like,

    while(sock->waitforreadyReady(300))

    {

    sock->read(f_name,sock->bytesavailable());

    -----

    }

    So i am recevieng the filenames till that timeout but server send all the file names. i want to receive all the file names from server untill server send some finsh string to client. so please help.

  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: Continously receive from server using waitforReadyread()

    For a start, don't use the synchronous functions, use the readyRead() signal to fire a slot. When you receive the signal take all the available data and append it to a QByteArray (that's declared as a member variable of the class not a local). If the QByteArray contains the end-of-list marker then process the data from the start of the array to the marker, remove that data from the array, and return from the slot with any remaining data still in the array.

  3. #3
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Continously receive from server using waitforReadyread()

    thnks for your reply m using readyread signal to fire my receiving slot.. so you mean to say i should use QByteArray to receive all the filenames..and what about loop then?can you plz explain me more?

  4. #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: Continously receive from server using waitforReadyread()

    Your loop is using the blocking waitForReadyRead() function, so it is incompatible with the typical asynchronous non-blocking process.

    The general idea (no error checking etc.) is in this code snippet:
    Qt Code:
    1. class Fetcher: public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Fetcher(QObject *p = 0): QObject(p), reply(0) {}
    6.  
    7. void fetch(const QUrl &url) {
    8. buffer.clear();
    9. reply = nam.get(QNetworkRequest(url));
    10. connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
    11. connect(reply, SIGNAL(readyRead()), SLOT(slotReadyRead()));
    12. }
    13.  
    14. public slots:
    15. void slotReadyRead() {
    16. buffer.append(reply.readAll());
    17.  
    18. int index = buffer.indexOf("\n\n"); // a blank line indicates the end
    19. if (index > 0) {
    20. // call something to process buffer.left(index)
    21.  
    22. // remove the processed data and the blank line
    23. buffer = buffer.mid(index+2);
    24. }
    25. }
    26.  
    27. void slotFinished() {
    28. reply->deleteLater();
    29. reply = 0;
    30. }
    31.  
    32. private:
    33. QNetworkAccessManager nam;
    34. QNetworkReply *reply;
    35. QByteArray buffer;
    36. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Continously receive from server using waitforReadyread()

    so far whatever i understood is buffer will receive all the filenames as a whole like,
    buffer[0]="filename1"
    buffer[1]="filename2"
    buffer[2]="filename3" etc..then it will check the index number of the last string so i no need send any finishing string.

    buffer.left[index] will give me the filename one by one. so can i copy this buffer.left[index] to any char buffer like strcpy(buffer1,buffer.left[index])?
    but i m not understanding what buffer = buffer.mid(index+2) is doing.

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Continously receive from server using waitforReadyread()

    Quote Originally Posted by rinku View Post
    so far whatever i understood is buffer will receive all the filenames as a whole like,
    buffer[0]="filename1"
    buffer[1]="filename2"
    buffer[2]="filename3" etc..then it will check the index number of the last string so i no need send any finishing string.

    buffer.left[index] will give me the filename one by one. so can i copy this buffer.left[index] to any char buffer like strcpy(buffer1,buffer.left[index])?
    but i m not understanding what buffer = buffer.mid(index+2) is doing.
    I am afraid this is completely wrong. "buffer" is a QByteArray, which is a simple container representing -- obviously -- an array of bytes. Its elements are bytes, not (pointers to) strings of characters.

    Are your server and client using raw TCP sockets to communicate? You seem to be assuming that each "sock->read()" operation will read exactly one file name, but this is not how TCP works. Structured data needs to be serialized and deserialized. I suggest you read this FAQ to understand the problem.

    ChrisW67's example does not use raw TCP sockets but the higher-level QNetworkAccessManager api (supporting http, ftp, etc.); nevertheless it shows how to deal with this "message framing" problem: it splits the incoming byte stream using the two-byte "\n\n" delimiter. Another solution would be to prefix each message with its length (see the FAQ).

  7. #7
    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: Continously receive from server using waitforReadyread()

    +1 what yeye_olive said.

    Just to expand a little. You have no control over exactly how the data you put in one end is broken up for transmission, just that TCP will deliver it all in the correct order. You might get all of one or both lists in the one packet... or you might not. So, you have to handle the worst case as in this contrived example. Let's say your servers sends the following data into the stream:
    Qt Code:
    1. filename1\nfilename2\nfilename3\n\nanotherset1\nanotherset2\n\n
    To copy to clipboard, switch view to plain text mode 
    then the receiver might get bits of data in chunks like this (packet per line):
    Qt Code:
    1. file
    2. name1\nf
    3. ilename\n
    4. 2\nfilename3\n\nanoth
    5. erset1
    6. \nanotherset2\n\n
    To copy to clipboard, switch view to plain text mode 

    So, you start with an empty buffer, then:
    • Packet 1 arrives and you add it to the buffer. There is no end-of-list mark in the buffer so you go back to waiting.
    • Packet 2 arrives and you add it to the buffer. There is no end-of-list mark in the buffer so you go back to waiting.
    • Packet 3 arrives and you add it to the buffer. There is no end-of-list mark in the buffer so you go back to waiting.
    • Packet 4 arrives and you add it to the buffer. There is an end-of-list mark in the buffer so you:
      • Extract the data from the start of the buffer to the end of list marker and process the complete first list
      • Remove the processed data from the buffer leaving "anoth" in the buffer
      • If there is another end-of-list marker in the buffer (possible, but not in this example) then repeat the preceding steps.
      • Go back to waiting
    • Packet 5 arrives and you add it to the buffer. There is no end-of-list mark in the buffer so you go back to waiting.
    • Packet 6 arrives and you add it to the buffer. There is an end-of-list mark in the buffer so you repeat the processing then go back to waiting.


    The exact mechanics of the processing are application specific and up to you. You might process a single file name when you see the end-of-line marker or wait for a whole list as I did.
    Last edited by ChrisW67; 21st August 2012 at 08:52.

  8. #8
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Continously receive from server using waitforReadyread()

    Yes i was wrong..thanks for your valuable replies..

Similar Threads

  1. Server don't receive a new request of client
    By tchoninho in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2011, 08:50
  2. Playing 2 wav files continously
    By HanyM.Magdy in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2010, 16:11
  3. Server can't receive RawData
    By Tadas in forum Newbie
    Replies: 3
    Last Post: 19th September 2010, 23:04
  4. reading Continously from QTextStream
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 2nd December 2008, 11:15
  5. Continously Scroll QPlainTextEdit
    By GimpMaster in forum Newbie
    Replies: 2
    Last Post: 12th September 2008, 17: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
  •  
Qt is a trademark of The Qt Company.