Results 1 to 10 of 10

Thread: duplicate data on TCPSocket

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: duplicate data on TCPSocket

    Confirmation:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include <QDebug>
    4. #include <QByteArray>
    5. #include <QDataStream>
    6.  
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9. #include <unistd.h>
    10. #include <time.h>
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QCoreApplication a(argc, argv);
    15.  
    16. qint32 random1;
    17. qint32 random2;
    18. qint32 oper;
    19.  
    20. //generate random numbers
    21. srand(time(NULL));
    22. random1=rand()%256;
    23. //sleep(3);
    24. srand(time(NULL));
    25. random2=rand()%256;
    26. srand((time(NULL)+5));
    27. oper=rand()%5;
    28.  
    29. qDebug() << "Test hex";
    30. qDebug() << "";
    31.  
    32. QByteArray array;
    33. QDataStream stream(&array,QIODevice::ReadWrite);
    34.  
    35. stream << (quint16)65502;
    36. array.clear();
    37. stream << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
    38.  
    39. qDebug() << "Hex =" << array.toHex();
    40.  
    41. QDataStream stream1(&array, QIODevice::ReadWrite);
    42. QDataStream stream2(&array, QIODevice::ReadWrite);
    43.  
    44. array.clear();
    45. stream1 << (quint16)65502;
    46. array.clear();
    47. stream2 << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
    48.  
    49. qDebug() << "Hex =" << array.toHex();
    50.  
    51. return a.exec();
    52. }
    To copy to clipboard, switch view to plain text mode 



    Results:
    Qt Code:
    1. Test hex
    2.  
    3. Hex = "ffdeffde5d5d01"
    4. Hex = "ffde5d5d01"
    To copy to clipboard, switch view to plain text mode 


    Edit: All I can say is that QDataStream is not meant to be used like this.

  2. The following user says thank you to tbscope for this useful post:

    daemonna (3rd August 2010)

Similar Threads

  1. Problem with receiving Data from TcpSocket
    By Basti300 in forum Qt Programming
    Replies: 0
    Last Post: 15th July 2010, 14:41
  2. QTcpSocket read duplicate data, but only on "bad channel"
    By creatron in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2010, 19:17
  3. TCPSocket/Server
    By TheGrimace in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 22:38
  4. Replies: 4
    Last Post: 8th January 2008, 18:41
  5. QProcess+TcpSocket
    By Fastman in forum Qt Programming
    Replies: 14
    Last Post: 13th November 2007, 11:34

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.