Results 1 to 4 of 4

Thread: Socket Reading Advice

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Socket Reading Advice

    Hi,

    I'm reading from a TCP socket with the code below, i have no knowledge of how much data will be sent, i just know that it will end in a specific termination sequence, so my pseudo code is:

    Read available bytes from socket
    check end of buffer for termination sequence
    Repeat if needed

    (im using synchronous sockets, so when the below function is called, i know there should be data ready to recieve)
    Qt Code:
    1. QByteArray buffer;
    2. QFile file(file_path);
    3.  
    4. if (!file.open(QIODevice::WriteOnly))
    5. return false;
    6.  
    7. do
    8. {
    9. buffer.clear();
    10.  
    11. if(!socket->waitForReadyRead())
    12. {
    13. socket->disconnect();
    14. qDebug() << "Error: "<< socket->errorString();
    15. file.close();
    16. return false;
    17. }
    18.  
    19. // read from socket
    20. buffer = socket->readAll();
    21.  
    22. if(buffer.size() == 0) // empty byte array = bad
    23. {
    24. socket->disconnect();
    25. qDebug("Error: Empty buffer.");
    26. file.close();
    27. return false;
    28. }
    29.  
    30. file.write(buffer); // Append current buffer chunk to file
    31.  
    32. // breaks when termination sequence found at end of buffer
    33. } while(!TransmissionFinished(buffer, type));
    34.  
    35. file.close(); // all done
    To copy to clipboard, switch view to plain text mode 

    While this works, after a bit of testing I occasionally get a timeout error which results in corrupt data because a chunk is missing, and there is no real reason why it should timeout in this case which makes me think im not approaching it correctly. So if anyone could tell me if something is clearly incorrect, or if there is a more elegant way of doing this i would really appreciate it.

    Many thanks for any help,

    Jack

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Socket Reading Advice

    Hi,

    I think that the loop that you are using is a bad idea. Thing on what happens if you call "buffer = socket->readAll()" but the socket has no data yet. A simple and best way to do this is to connect the "readyRead()" signal from the socket to a slot on you will have to check if all the data has been arrived and then close the socket connection.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Socket Reading Advice

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    I think that the loop that you are using is a bad idea. Thing on what happens if you call "buffer = socket->readAll()" but the socket has no data yet. A simple and best way to do this is to connect the "readyRead()" signal from the socket to a slot on you will have to check if all the data has been arrived and then close the socket connection.
    Thanks, but does my blocking call to waitForReadyRead() not guarantee that when it returns with true, there will be data waiting to be read by socket->readAll()?

    I would much rather take the blocking synchronous approach that using async signals/slots in this case if at all possible.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Socket Reading Advice

    Hi,

    I said "readyRead()" signal, not "waitForReadyRead(int msecs)" method. Take a look at "fortuneclient" example.

    I would much rather take the blocking synchronous approach that using async signals/slots in this case if at all possible.
    Really? Don't know why.
    Òscar Llarch i Galán

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19: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.