Results 1 to 10 of 10

Thread: duplicate data on TCPSocket

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

    Default duplicate data on TCPSocket

    i'm getting duplicate data on client-server communication... when sending for example 65502 (hex ffde), on socket i can see duplicate (ffdeffde..)..

    to client.. wanna auth? client wanna authenticate???
    question 67 op 1 67 = -122
    "ffdeffde434301"
    getting auth answer from client... checking


    asking for authentication??? writing authme
    sending auth question??? "ffdeffde4343010032000000010000000a000500e0010c00e 8e83d0010e83d0010e83d0010e83d0"
    writing autha
    auth passed


    SERVER:


    QByteArray auth;
    QDataStream dsauth(&auth,QIODevice::ReadWrite);
    ....etc

    indg_socket::indg_socket(QObject *parent) :
    QTcpSocket(parent)
    {
    //generate random numbers
    srand(time(NULL));
    random1=rand()%256;
    //sleep(3);
    srand(time(NULL));
    random2=rand()%256;
    srand((time(NULL)+5));
    oper=rand()%5;

    result=computeauth(random1, random2, oper); //put 2 random numbers, random operation and compute result


    dsauth << (quint16)65500;
    dsauthme << (quint16)65501;
    dsauthq << (quint16)65502;
    dsautha << (quint16)65503;
    dsuserkeys << (quint16)65504;
    dsauthok << (quint16)65508;
    dsauthfailure << (quint16)65509;
    dsadmstatq << (quint16)65510;

    isClientAuthenticated=false;
    connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
    connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    }

    void indg_socket::readpackets()
    {
    sockdata=this->readAll();
    sockdata.truncate(100); //truncate if packet greater than 100 bytes

    if(sockdata==authme){
    qDebug() << "client wanna authenticate!!!";
    qDebug() << "sending question " << random1 << " op" << oper << " " << random2 << " = " << result;
    authq.clear();
    dsauthq << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
    qDebug() << authq.toHex();
    this->write(authq, 40);
    sockdata.clear();
    return;
    }

    if(sockdata==autha){
    qDebug() << "getting auth answer from client... checking";
    qDebug() << "OK";
    this->write(authok);
    sockdata.clear();
    return;
    }

    if(sockdata.startsWith(admstatq)){
    qDebug() << "getting auth answer from client... checking";
    qDebug() << "OK";
    this->write(authok);
    sockdata.clear();
    return;
    }

    return;
    }


    CLIENT:


    ait_socket::ait_socket(QObject *parent) :
    QTcpSocket(parent)
    {
    dsauth << (quint16)65500;
    dsauthme << (quint16)65501;
    dsauthq << (quint16)65502;
    dsautha << (quint16)65503;
    dsuserkeys << (quint16)65504;
    dsauthok << (quint16)65508;
    dsauthfailure << (quint16)65509;

    connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
    connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    }

    void ait_socket::readpackets()
    {
    sockdata=this->readAll();

    if(sockdata.startsWith(auth)){
    qDebug() << "server asking for authentication!!!";
    qDebug() << "OK, writing authme";
    this->write(authme);
    sockdata.clear();
    return;
    }

    if(sockdata.startsWith(authq)){
    qDebug() << "server sending auth question!!!";
    qDebug() << sockdata.toHex();
    qDebug() << "OK, writing autha";
    this->write(autha);
    sockdata.clear();
    return;
    }

    if(sockdata.startsWith(authok)){
    qDebug() << "auth passed!";
    this->write(userkeys);
    sockdata.clear();
    return;
    }
    return;
    }

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

    Default Re: duplicate data on TCPSocket

    i bet the problem is with my code, but after few days lookin at it, i didnt find problem... so PLEASE any help is really appreciated.

    thanx in advance

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

    Default Re: duplicate data on TCPSocket

    The code isn't easy to follow and it contains variables I don't see defined, like authq

    You you edit the code to include the complete header and implementation of the client and server?
    And canu place the code between code tags. It's the # icon in the toolbar.

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

    Default Re: duplicate data on TCPSocket

    Header:

    code Code:
    1. #ifndef INDG_SOCKET_H
    2. #define INDG_SOCKET_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QtGlobal>
    6.  
    7. class indg_socket : public QTcpSocket
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit indg_socket(QObject *parent = 0);
    12. qint32 computeauth(qint32 random1, qint32 random2, qint32 oper);
    13.  
    14. protected:
    15. void readyRead();
    16.  
    17. private slots:
    18. void readpackets();
    19. };
    20.  
    21. #endif // INDG_SOCKET_H
    To copy to clipboard, switch view to plain text mode 

    CPP:

    code Code:
    1. #include "indg_socket.h"
    2.  
    3. #include <QtNetwork>
    4. #include <QtDebug>
    5. #include <QByteArray>
    6. #include <QtGlobal>
    7.  
    8. #include <stdio.h>
    9. #include <stdlib.h>
    10. #include <unistd.h>
    11. #include <time.h>
    12.  
    13. bool isClientAuthenticated;
    14. qint32 random1;
    15. qint32 random2;
    16. qint32 oper;
    17. qint32 result;
    18.  
    19. QByteArray auth; //65500 do you want to authenticate?
    20. QByteArray authme; //65501 authenticate me
    21. QByteArray authq; //65502 auth question for client
    22. QByteArray autha; //65503 auth answer for server
    23. QByteArray userkeys;//65504 i'm sending user keys
    24. QByteArray wmp; //65505 whole map for client
    25. QByteArray partd; //65506 partial data for client
    26.  
    27. QByteArray authok; //65508 auth OK
    28. QByteArray authfailure; //65508 auth FAILED
    29. QByteArray admstat; //65509 admin stats
    30. QByteArray admstatq; //65510 admin stats query
    31.  
    32. QByteArray sockdata;
    33. QDataStream dsauth(&auth,QIODevice::ReadWrite);
    34. QDataStream dsauthme(&authme,QIODevice::ReadWrite);
    35. QDataStream dsauthq(&authq,QIODevice::ReadWrite);
    36. QDataStream dsautha(&autha,QIODevice::ReadWrite);
    37. QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
    38. QDataStream dswmp(&wmp,QIODevice::ReadWrite);
    39. QDataStream dspartd(&partd,QIODevice::ReadWrite);
    40. QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
    41. QDataStream dsadmstatq(&admstatq,QIODevice::ReadWrite);
    42.  
    43. QDataStream dsauthok(&authok,QIODevice::ReadWrite);
    44. QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
    45.  
    46. indg_socket::indg_socket(QObject *parent) :
    47. QTcpSocket(parent)
    48. {
    49. //generate random numbers
    50. srand(time(NULL));
    51. random1=rand()%256;
    52. //sleep(3);
    53. srand(time(NULL));
    54. random2=rand()%256;
    55. srand((time(NULL)+5));
    56. oper=rand()%5;
    57.  
    58. result=computeauth(random1, random2, oper);
    59.  
    60.  
    61. dsauth << (quint16)65500;
    62. dsauthme << (quint16)65501;
    63. dsauthq << (quint16)65502;
    64. dsautha << (quint16)65503;
    65. dsuserkeys << (quint16)65504;
    66. dsauthok << (quint16)65508;
    67. dsauthfailure << (quint16)65509;
    68. dsadmstatq << (quint16)65510;
    69.  
    70. isClientAuthenticated=false;
    71. connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
    72. connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    73. }
    74.  
    75. void indg_socket::readpackets()
    76. {
    77. sockdata=this->readAll();
    78. sockdata.truncate(100); //truncate if packet greater than 100 bytes
    79.  
    80. if(sockdata==authme){
    81. qDebug() << "client wanna authenticate!!!";
    82. qDebug() << "sending question " << random1 << " op" << oper << " " << random2 << " = " << result;
    83. authq.clear();
    84. dsauthq << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
    85. qDebug() << authq.toHex();
    86. this->write(authq, 40);
    87. sockdata.clear();
    88. return;
    89. }
    90.  
    91. if(sockdata==autha){
    92. qDebug() << "getting auth answer from client... checking";
    93. qDebug() << "OK";
    94. this->write(authok);
    95. sockdata.clear();
    96. return;
    97. }
    98.  
    99. if(sockdata.startsWith(admstatq)){
    100. qDebug() << "getting auth answer from client... checking";
    101. qDebug() << "OK";
    102. this->write(authok);
    103. sockdata.clear();
    104. return;
    105. }
    106.  
    107. return;
    108. }
    109.  
    110. qint32 indg_socket::computeauth(qint32 random1, qint32 random2, qint32 oper)
    111. {
    112. qint8 res;
    113. switch(oper){
    114. case 1:
    115. res=random1+random2;
    116. return res;
    117. case 2:
    118. res=random1-random2;
    119. return res;
    120. case 3:
    121. res=random1*random2;
    122. return res;
    123. case 4:
    124. res=random1/random2;
    125. return res;
    126. }
    127. }
    To copy to clipboard, switch view to plain text mode 

    client side


    code Code:
    1. #ifndef AIT_SOCKET_H
    2. #define AIT_SOCKET_H
    3.  
    4. #include <QTcpSocket>
    5.  
    6. class ait_socket : public QTcpSocket
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit ait_socket(QObject *parent = 0);
    11.  
    12. protected:
    13. void readyRead();
    14.  
    15. private slots:
    16. void readpackets();
    17. };
    18.  
    19. #endif // AIT_SOCKET_H
    To copy to clipboard, switch view to plain text mode 

    CPP

    code Code:
    1. #include "ait_socket.h"
    2.  
    3. #include <QtNetwork>
    4. #include <QtDebug>
    5. #include <QByteArray>
    6. #include <QtGlobal>
    7. #include <stdio.h>
    8.  
    9. QByteArray auth; //65500 do you want to authenticate?
    10. QByteArray authme; //65501 authenticate me
    11. QByteArray authq; //65502 auth question for client
    12. QByteArray autha; //65503 auth answer for server
    13. QByteArray userkeys;//65504 i'm sending user keys
    14. QByteArray wmp; //65505 whole map for client
    15. QByteArray partd; //65506 partial data for client
    16.  
    17. QByteArray authok; //65508 auth OK
    18. QByteArray authfailure; //65508 auth FAILED
    19. QByteArray admstat; //65509 admin stats
    20. QByteArray admstatq; //65510 admin stats query
    21.  
    22. QByteArray sockdata;
    23. QDataStream dsauth(&auth,QIODevice::ReadWrite);
    24. QDataStream dsauthme(&authme,QIODevice::ReadWrite);
    25. QDataStream dsauthq(&authq,QIODevice::ReadWrite);
    26. QDataStream dsautha(&autha,QIODevice::ReadWrite);
    27. QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
    28. QDataStream dswmp(&wmp,QIODevice::ReadWrite);
    29. QDataStream dspartd(&partd,QIODevice::ReadWrite);
    30. QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
    31.  
    32. QDataStream dsauthok(&authok,QIODevice::ReadWrite);
    33. QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
    34.  
    35.  
    36. ait_socket::ait_socket(QObject *parent) :
    37. QTcpSocket(parent)
    38. {
    39. dsauth << (quint16)65500;
    40. dsauthme << (quint16)65501;
    41. dsauthq << (quint16)65502;
    42. dsautha << (quint16)65503;
    43. dsuserkeys << (quint16)65504;
    44. dsauthok << (quint16)65508;
    45. dsauthfailure << (quint16)65509;
    46.  
    47. connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
    48. connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    49. }
    50.  
    51. void ait_socket::readpackets()
    52. {
    53. sockdata=this->readAll();
    54.  
    55. if(sockdata.startsWith(auth)){
    56. qDebug() << "server asking for authentication!!!";
    57. qDebug() << "OK, writing authme";
    58. this->write(authme);
    59. sockdata.clear();
    60. return;
    61. }
    62.  
    63. if(sockdata.startsWith(authq)){
    64. qDebug() << "server sending auth question!!!";
    65. qDebug() << sockdata.toHex();
    66. qDebug() << "OK, writing autha";
    67. this->write(autha);
    68. sockdata.clear();
    69. return;
    70. }
    71.  
    72. if(sockdata.startsWith(authok)){
    73. qDebug() << "auth passed!";
    74. this->write(userkeys);
    75. sockdata.clear();
    76. return;
    77. }
    78. return;
    79. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: duplicate data on TCPSocket

    btw. dont worry about computeauth() and use of it in communication... this will be fixed as soon as mystery of duplicate data is sorted out...

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    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 ?

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

    daemonna (3rd August 2010)

  9. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    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.

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

    daemonna (3rd August 2010)

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

    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???

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.