Results 1 to 4 of 4

Thread: QTcpSocket get disconnected when server is sending huge data

  1. #1
    Join Date
    Jun 2010
    Location
    Dhaka, Bangladesh
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket get disconnected when server is sending huge data

    Before describing my problem, let me first give you the background of my application. There is a tcp server (which is written in Delpi) and a tcp client which I am writing in C++ (Qt). When the client connects to the server, the server sends a large chunk of configuration data to the client (about 3 MB). The problem I am having is that the TCP connection is being disconnected during the receive of this large chunk of data.

    I've tried (temporarily) reducing the data size down to a few hundred Kbytes, and sometimes it works, sometimes it fails. It seems to fail more regularly with slower PCs. If I reduce it to 10KB, it works 100% of the time, but I need to be able to have it work with the 3MB of data the host will normally send. I have also experimented with having the host wait for a minute before sending the large chunk of data, and send little chunks first. The smaller chunks (a few KB) work fine, but the disconnect still happens when the large chunk comes in. Also, the number of bytes received before the connection is broken seems to vary randomly.

    The server is (currently) running on the same machine, and we are using the loopback address (127.0.0.1) as the connection IP address.

    The following is the slot which signaled when data is received:

    Qt Code:
    1. void Socket::dataReadyToRead()
    2. {
    3. /*
    4. * Let us lock this slot so that further hitting on this cause wait while it is
    5. * already hitted.
    6. */
    7. QMutexLocker locker(&mMutex);
    8.  
    9. /*
    10. * We will only read data when at least one command is arrived. Here is the logic...
    11. * We will continue till the length of the next command is arrived. If only length is
    12. * arrived (not content yet), just read it in d->mnBlockSize (NOTE: LSB come first, i.e, little endian),
    13. * leave this method. But if content arrived too, let us read it and process this command.
    14. */
    15. while(bytesAvailable() >= (int)sizeof(quint32)){
    16. if (0 == mnBlockSize){
    17. /*
    18. * Read the length bytes first. And as LSB come first, need to calc length like following.
    19. */
    20. char length_byte[4];
    21. read(length_byte,4);
    22.  
    23. mnBlockSize = length_byte[0];
    24. mnBlockSize += (length_byte[1] * 256);
    25. mnBlockSize += (length_byte[2] * 256 * 256);
    26. mnBlockSize += (length_byte[3] * 256 * 256 * 256);
    27. }
    28.  
    29. /*
    30. * Content is not available yet, so there is nothing to do.
    31. */
    32. if (bytesAvailable() < mnBlockSize){
    33. return;
    34. }
    35.  
    36. /*
    37. * Content arrived, so get it, copy it to a buffer, analyze the packet by calling
    38. * prepareCommand and them emit the prepared packet to the system so that
    39. * contorller get it and process it.
    40. */
    41. char dataArrived[MAX_DATA_LENGTH];
    42. read(dataArrived,mnBlockSize);
    43.  
    44. if(mnBlockSize <= MAX_DATA_LENGTH){
    45. QByteArray com(dataArrived,mnBlockSize);
    46. emit commandCame(com);
    47. }
    48.  
    49. mnBlockSize = 0;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    Following is the thread run method’s code.
    Qt Code:
    1. void SocketThread::run()
    2. {
    3. if(mTcpHandler){
    4. Socket tcpClient;
    5. connect(mTcpHandler, SIGNAL(connectToBobHost(QString, int)), &tcpClient, SLOT(connectToBobHost(QString, int)));
    6. connect(mTcpHandler, SIGNAL(disconnectFromBobHost()), &tcpClient, SLOT(disconnectFromBobHost()), Qt::BlockingQueuedConnection);
    7. connect(mTcpHandler, SIGNAL(dataReadyToSend(QByteArray)), &tcpClient, SLOT(sendData(QByteArray)));
    8.  
    9. connect(&tcpClient, SIGNAL(hostConnected()), mTcpHandler, SLOT(hostConnected()));
    10. connect(&tcpClient, SIGNAL(hostDisconnected(bool )), mTcpHandler, SLOT(hostDisconnected(bool)), Qt::BlockingQueuedConnection);
    11. connect(&tcpClient, SIGNAL(hostNotConnected(const QString&)), mTcpHandler, SLOT(hostNotConnected(const QString&)));
    12. connect(&tcpClient, SIGNAL(commandCame(QByteArray)), mTcpHandler, SLOT(analyzeCommand(QByteArray)));
    13. exec();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 


    Can anyone help please?

  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: QTcpSocket get disconnected when server is sending huge data

    First of all you don't need the mutex. Second of all who is breaking the connection - the server or the client? Third of all make sure you get correct values for the block size on the client, your way of assembling it is quite strange, usually one uses bit shifting for it or qFromLittleEndian().
    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
    Jun 2010
    Location
    Dhaka, Bangladesh
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket get disconnected when server is sending huge data

    Thank you very much wysota for replying.

    - Yes, I tried even without mutex-ing.

    - I see in the client side that, the disconnnected signal of Socket is being invoked and the error at that time was 'RemoteHostClosedError'.
    Qt Code:
    1. void Socket::onHostDisconnected()
    2. {
    3. emit hostDisconnected(error() != QAbstractSocket::UnknownSocketError);
    4. }
    To copy to clipboard, switch view to plain text mode 
    I saw that the code returns by 'error()' is 'RemoteHostClosedError'. Note that this is my problem.

    - Yes, qFromLittleEndian works for me too (thank you), but mine one is ok too.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket get disconnected when server is sending huge data

    QAbstractSocket::RemoteHostClosedError means that the server closed the socket while you're receiving data.

    I suggest you to use a LanSniffer (Wireshark) to see what the server does when it send you data.
    Last edited by wysota; 1st April 2011 at 09:36.
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 6
    Last Post: 24th February 2012, 11:17
  2. Replies: 8
    Last Post: 18th December 2010, 15:07
  3. [TCP Server] Server only sending when terminating
    By papperlapapp in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2010, 19:41
  4. QFtp disconnected from server
    By cutie.monkey in forum Newbie
    Replies: 5
    Last Post: 20th May 2009, 11:46
  5. QTcpSocket - How do I know when it is disconnected ?
    By probine in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2006, 21:05

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.