Results 1 to 17 of 17

Thread: QTcpSocket write data with packet loss - Idiot proof sample does not work!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Unhappy QTcpSocket write data with packet loss - Idiot proof sample does not work!

    I have big Qt, Linux, C++ and Web programming experience, but this case blows up my brain!

    This code works on many sites, many different machines with many different XML files, but does not work with some files on some machines without the rules. I have tried everything, blocking and non-blocking sockets, w and w/o socket flush.

    On server side, I dump bad TCP stream with tcpdump eg.
    Qt Code:
    1. tcpdump -w <bin_dump.log> host <client_IP>
    To copy to clipboard, switch view to plain text mode 
    and then analyze them in Wireshark. As I can understand server missed one packet (maybe the first one) and could not receive the whole message.

    On client side, I made binary dump of TCP stream sent and send it with
    Qt Code:
    1. dd if=./tcp.bin > /dev/tcp/<server_ip>/<server_port>
    To copy to clipboard, switch view to plain text mode 
    and everything is fine then. So, the problem is in my client side app.

    Is such a cases the only one solution is to manually add one extra char into XML tag and change the size of the file, eg. 7604 bytes to 7605 bytes, and then the file was sent. But, the files smaller and bigger that the one can be sent w/o problems. There is no rules about size. Sometimes it happens on file with 823 bytes, sometimes with 12666 bytes.

    Relevant code is:
    Qt Code:
    1. /**
    2.   * Send XML file
    3.   * @param sMsg Message prefix
    4.   * @param sFile XML file name
    5.   */
    6. void TXmlSender::tcpSendXmlFile(const QString& sMsg, const QString& sFileName)
    7. {
    8. QString sXml;
    9. xmlFileToString(sFileName, sXml);
    10.  
    11. TXmlTCPCli *tcpCli = new TXmlTCPCli(m_sHostIP, m_nHostPort, this);
    12. connect(tcpCli, SIGNAL(onResponse(const QString&, const QString&)),
    13. this, SLOT(slot_onSendXmlFile(const QString&, const QString&)));
    14.  
    15. QString sSend = m_sLokalID + ": " + sMsg + " " + sXml;
    16. tcpCli->sendMsgReq(sSend);
    17. }
    To copy to clipboard, switch view to plain text mode 

    TXmlTCPCli is trivial TCP client class and main part of them is:
    Qt Code:
    1. /**
    2.   * SLOT: On connected to host
    3.   */
    4. void TXmlTCPCli::onConnected()
    5. {
    6. QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    7. if (socket == 0) {
    8. return;
    9. }
    10. QByteArray bArrMsg;
    11. QDataStream out(&bArrMsg, QIODevice::WriteOnly);
    12. out.setVersion(QDataStream::Qt_4_0);
    13.  
    14. out << (quint16)0;
    15. out << m_sMsg;
    16. out.device()->seek(0);
    17. out << (quint16)(bArrMsg.size() - (int)sizeof(quint16));
    18.  
    19. while (!bArrMsg.isEmpty()) {
    20. if (!socket->isWritable()) {
    21. LOG("Error! Could not write to socket2!");
    22. break;
    23. }
    24. qint32 sentNow = socket->write(bArrMsg);
    25. if (sentNow < 0) {
    26. LOG("Error! sendMsgImpl: Could not write to socket!");
    27. break;
    28. }
    29. else {
    30. bArrMsg.remove(0, sentNow);
    31. // socket->flush();
    32. LOG("sendMsgImpl: Sent " << sentNow << ", Rest: " << bArrMsg.size());
    33. }
    34. }
    35. socket->disconnectFromHost();
    36. }
    37.  
    38. /**
    39.   */
    40. void TXmlTCPCli::onDisconnected()
    41. {
    42. QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    43. if (socket != 0) {
    44. socket->deleteLater();
    45. }
    46. this->deleteLater();
    47. }
    To copy to clipboard, switch view to plain text mode 

    When this code catch some files, server waits, and waits and then timeout and disconnect. The rest of the code works fine, and send TCP messages, receive TCP messages etc. This application also has QTcpServer listen on same port. Client machines was Mint32 Maya, Ubuntu 11.10 and Qt is v 4.8.1.

    How to resolve this? How to debug QTcpSocket on lower level? Why many files sent normally but other only with change the size? Qt bug? Memory leak? Any hint?
    Attached Files Attached Files
    Last edited by jboban; 25th March 2013 at 13:26.

Similar Threads

  1. Replies: 0
    Last Post: 18th August 2011, 04:36
  2. QTcpSocket write():: How many data sends?
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2010, 10:27
  3. QTcpSocket write() Trasmission Data Design
    By Daxos in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2010, 18:19
  4. Packet handling advice with QTcpSocket?
    By zAAm in forum General Programming
    Replies: 1
    Last Post: 27th April 2010, 15:23
  5. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 5th August 2009, 23:11

Tags for this Thread

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.