Results 1 to 2 of 2

Thread: QTcpsocket fast write data problem

  1. #1
    Join Date
    Feb 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QTcpsocket fast write data problem

    I wrote the simple Tcp client-server app.
    I have a Packet class, look like:
    First 4 byte are the data type, and other are Data (QString) as Bytearray.

    I have 3 threads.
    Server -> User Manager -> Socket Thread.
    So, when i try this ( for test ) :

    Qt Code:
    1. for(int i = 0; i < 5;i++){
    2. Packet * teszt1 = new Packet;
    3. teszt1->setType(12);
    4. teszt1->setData("Something data teszt1 packet")
    5.  
    6. Packet * teszt2 = new Packet;
    7. teszt2->setType(21);
    8. teszt2->setData("Something data teszt2 packet")
    9.  
    10. QMetaObject::invokeMethod(usersocket,"writeData",Qt::QueuedConnection,Q_ARG(Packet*,teszt1));
    11.  
    12. QMetaObject::invokeMethod(usersocket,"writeData",Qt::QueuedConnection,Q_ARG(Packet*,teszt2));
    13. }
    To copy to clipboard, switch view to plain text mode 
    The package arrive ,but the teszt2 packet's type equal to the teszt1. (The data is different.)

    BUT!!! When i add new line to my writeData method: QThread::msleep(30); It's work!
    Why???


    My writeData method:

    Qt Code:
    1. void socketThread::writeData(Packet * packet)
    2. {
    3. if(Socket->state() == QAbstractSocket::ConnectedState)
    4. {
    5. qDebug() << "Type: " << packet->getType(); //
    6. qDebug() << "Data: " << packet->getStringData();
    7. QByteArray kuldendo;
    8. *packet >> kuldendo;
    9. QByteArray Packetsize= Packet::IntToArray(packet->sizeOfPacket());
    10. Socket->write(Packetsize); //Sizeof(Packet type + Packet data)
    11. Socket->write(kuldendo); //Packet type + Packet data
    12. Socket->waitForBytesWritten();
    13. QThread::msleep(30); // <- without not work.
    14. }
    15. delete packet;
    16. }
    To copy to clipboard, switch view to plain text mode 

    The client-side output without QThread::msleep(30) show :

    Qt Code:
    1. ---------------------
    2. Packet Type: 12
    3. Packet Data: "Something data teszt1 packet"
    4. ---------------------
    5. NEW MESSAGE FROM SERVER
    6. ---------------------
    7. Packet Type: 12
    8. Packet Data: "Something data teszt2 packet"
    9. ---------------------
    10. NEW MESSAGE FROM SERVER
    11. ---------------------
    12. Packet Type: 12
    13. Packet Data: "Something data teszt1 packet"
    14. ---------------------
    15. NEW MESSAGE FROM SERVER
    16. ---------------------
    17. Packet Type: 12
    18. Packet Data: "Something data teszt2 packet"
    19. ---------------------
    20. NEW MESSAGE FROM SERVER
    21. ---------------------
    22. Packet Type: 12
    23. Packet Data: "Something data teszt1 packet"
    24. ---------------------
    25. NEW MESSAGE FROM SERVER
    26. ---------------------
    27. Packet Type: 12
    28. Packet Data: "Something data teszt2 packet"
    29. ---------------------
    30. NEW MESSAGE FROM SERVER
    31. ---------------------
    32. Packet Type: 12
    33. Packet Data: "Something data teszt1 packet"
    34. ---------------------
    35. NEW MESSAGE FROM SERVER
    36. ---------------------
    37. Packet Type: 12
    38. Packet Data: "Something data teszt2 packet"
    39. ---------------------
    40. NEW MESSAGE FROM SERVER
    41. ---------------------
    42. Packet Type: 12
    43. Packet Data: "Something data teszt1 packet"
    44. ---------------------
    45. NEW MESSAGE FROM SERVER
    46. ---------------------
    47. Packet Type: 12
    48. Packet Data: "Something data teszt2 packet"
    To copy to clipboard, switch view to plain text mode 

    I use Qt 5.7 with Static mingw compiler.
    (OS: Windows 10)

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpsocket fast write data problem

    Have you verified that the problem is on the writing side not on the reading side?

    Cheers,
    _

    P.S.: any specific reason for such a complicated setup?

Similar Threads

  1. Replies: 16
    Last Post: 27th March 2013, 06:23
  2. fast read write mssql information from remot server
    By hamidarr in forum Qt Programming
    Replies: 4
    Last Post: 22nd July 2011, 21:02
  3. QTcpSocket write():: How many data sends?
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2010, 10:27
  4. QTcpSocket write() Trasmission Data Design
    By Daxos in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2010, 18:19
  5. Replies: 6
    Last Post: 8th January 2007, 10:24

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.