Results 1 to 10 of 10

Thread: duplicate data on TCPSocket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

    One problem is this line:

    Qt Code:
    1. this->write(authq, 40);
    To copy to clipboard, switch view to plain text mode 
    This makes an implicite conversion from QByteArray to const char*
    For some reason this works, but I'm sure it won't give you what you want.

    This might not be the problem why you get things double though.


    Another thing that's not 100% correct is the initialisation of your random number generators.

  2. #2
    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

    And the probable reason why you get a double auth code is because you don't flush the stream.
    You do clear the bytearray though.

    Note that this is a pure guess though!

    In short: this is what happens

    create bytearray authq
    create datastream dsauthq which writes and reads on bytearray authq

    by default (in the constructor) already write (quint16)65502 via the data stream to the bytearray.
    clear the bytearray
    The stream still contains the code
    Now write the code again with the random numbers and operator.

    In effect, you write the code twice.

    The fun thing is, QDatastream doesn't have a flush() function.
    Delete the stream and create a new one on the go. That will definatly solve stream buffer problems.

    I would do this very differently though but I assume you can not change the requirements of (quint16)65502 ?

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

    daemonna (3rd August 2010)

  4. #3
    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.

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

    daemonna (3rd August 2010)

  6. #4
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: duplicate data on TCPSocket

    thanks for all your replies...

    Edit: All I can say is that QDataStream is not meant to be used like this.
    what else do you suggest if i need to work with 'raw data', not a text???

  7. #5
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: duplicate data on TCPSocket

    yeah, you were exactly right.. i had to use separate qbytearray to send thru socket, and mentioned bytearrays use only for comparison (sockdata.startsWith(auth)) + i have to use .device()->seek(0) all the time to make sure nothing left in bytearray, coz i've noticed packet was several times OK, but suddenly i found extra zeros in beginning of array..

    THANKS

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.