Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 47

Thread: QTcpSocket readyRead strange behavior

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    You are sending raw bytes and interpreting them on the other end as integers. Don't do that. I told you to encode the size explicitly. If your sender is a 32 bit OS and your receiver a 64 bit OS (or the other way round) your code will fail immediately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #22
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    you're totally right, but for this example, that's not the point : the code does not fail immediately.
    As i am on 2 32bits windows, it works.

    (does the size of "short" change between 32 and 64bit OS? i though it could be a problem only if the endianness changed)

  3. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    It is the point because it is this exact variable that is giving you an incorrect result. So first make sure the problem is not there (and not assume it is not there) and then continue elsewhere.

    This is sender side compatible with my previous suggestion.

    Qt Code:
    1. QByteArray data; // external byte array containing your data to be sent
    2.  
    3. quint16 s = data.size();
    4. QByteArray ba(2, 0);
    5. ba[0] = (s >> 8) & 0xFF;
    6. ba[1] = s & 0xFF;
    7. ba.append(data);
    8. for (i=0;i<vecClient.size();++i)
    9. {
    10. vecClient[i]->write(ba);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #24
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    i've got the same problem.

    receiver side code :
    Qt Code:
    1. void Client::onSocketReadyRead()
    2. {
    3. bufferSocket += pSocket->readAll();
    4.  
    5. while (bufferSocket.size() >= 2)
    6. {
    7. quint16 size = (bufferSocket.at(0) << 8) + bufferSocket.at(1); // 16bit unsigned value
    8.  
    9. qDebug()<<"size="<<size;
    10.  
    11. if (size != 19998)
    12. {
    13.  
    14. QMessageBox::critical(NULL, tr("error"), tr("This is it"));
    15. pSocket->disconnectFromHost();
    16. return;
    17. }
    18.  
    19. if(bufferSocket.size() <= 2+size) return;
    20.  
    21. qDebug()<<"data read";
    22.  
    23. QByteArray data = bufferSocket.mid(2, size);
    24.  
    25. bufferSocket.remove(0,2+size);
    26. }
    To copy to clipboard, switch view to plain text mode 

    sender side code:
    Qt Code:
    1. void Server::onTimeout()
    2. {
    3. QByteArray data;
    4. int i;
    5.  
    6. data.fill(1, 19998);
    7.  
    8. quint16 s = data.size();
    9. QByteArray ba(2, 0);
    10. ba[0] = (s >> 8) & 0xFF;
    11. ba[1] = s & 0xFF;
    12. ba.append(data);
    13. for (i=0;i<vecClient.size();++i)
    14. {
    15. vecClient[i]->write(ba);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  5. #25
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Dump the data you receive on the receiving end to see whether you are not losing synchronization with your stream.

    By the way, if your data is of constant size then why are you sending the size with each bucket?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #26
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    the data is of constant size just for these tests. It will change in the real project

    I think indeed i've a synchronisation problem. How can it be, as i am in TCP mode?
    what do you mean by dump?
    should i make a readAll after receiving a full bucket?

  7. #27
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Quote Originally Posted by naroin View Post
    I think indeed i've a synchronisation problem. How can it be, as i am in TCP mode?
    If you read less data than you write then you lose synchronisation.

    what do you mean by dump?
    Display on console.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #28
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    that is strange.

    logging code:
    Qt Code:
    1. QString strDebug;
    2. quint8 c, c2;
    3. int nCount = 0;
    4. c2 = data.at(0);
    5. for (i=1;i<data.size();++i)
    6. {
    7. ++nCount;
    8. c = data.at(i);
    9. if (c != c2)
    10. {
    11. strDebug += tr("%1 * %2 | ").arg(c2).arg(nCount);
    12.  
    13. c2 = c;
    14. nCount = 0;
    15. }
    16.  
    17. }
    18. ++nCount;
    19. strDebug += tr("%1 * %2 | ").arg(c2).arg(nCount);
    20. qDebug()<<strDebug;
    To copy to clipboard, switch view to plain text mode 

    logs:
    ...
    size= 19998
    data read
    "1 * 19998 | "
    size= 19998
    size= 19998
    size= 19998
    data read
    "1 * 12494 | 78 * 1 | 30 * 1 | 1 * 7502 | "



    i should have only "1" in my data, but i dont have.
    78/30 is 4E1E = 19998, that's my size.
    it is as if some data were dropped after my 12494 bytes

    the server side sends correct data (1* 19998).
    Last edited by naroin; 19th January 2011 at 15:29.

  9. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Quote Originally Posted by naroin View Post
    the server side sends correct data (1* 19998).
    How do you know that? Do you check the return value of QIODevice::write()? Do you also check the how many bytes are really read on the receiving end?

    By the way, get rid of threads, you don't need them and they might be influencing your results somehow.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #30
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    i just changed the receiver side code with windows sockets : it works perfectly.
    So
    * that's not a sender side problem
    * that's not a thread problem
    * i think there may be a bug in QTcpSocket::readyRead, but that's very strange as it seems i'm the only guy in the world to have it.

    thanks for your help

  11. The following user says thank you to naroin for this useful post:

    Juba (29th April 2011)

  12. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    The bug is most likely in the way you use the library. You couldn't have modified your code "in-place", you must have rewritten many aspects of your program changing the way your application works. Somehow strangely thousands of applications work with QTcpSocket without problems and suddenly you believe that there is a bug there because in your application something doesn't work. Think yourself what is more probable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #32
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    yes that's what i think, but noone here seems to be able to say me what is wrong !

    i used the code you sent me, and i got the same problem.
    My test applications don't do anything than launching the threads whom i've sent you the code. I really dont understand what can go wrong!

    what do you want i send you so you can be able to say me what i did wrong? i really want to know, and you're right, i think it's my fault, but i dont know why.

    maybe a problem in the initialization ?
    Qt Code:
    1. void Client::run()
    2. {
    3. pSocket = new QTcpSocket(this);
    4.  
    5. connect(pSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
    6.  
    7. pSocket->connectToHost("192.168.50.77", 1234);
    8. if (!pSocket->waitForConnected())
    9. {
    10. qDebug()<<"Unable to connect";
    11. return;
    12. }
    13.  
    14. exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by naroin; 20th January 2011 at 08:34.

  14. #33
    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: QTcpSocket readyRead strange behavior

    Note that the slot you call from the thread is not performed inside the thread.

  15. #34
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    how can i do so it is in the thread?
    This is one of the things i don't understand well about Qt & threads.


    Added after 26 minutes:


    i've created a new class ClientInternal, and in my Client code:
    Qt Code:
    1. void Client::run()
    2. {
    3. ClientInternal ci;
    4.  
    5. exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    my ClientInternal code:
    Qt Code:
    1. ClientInternal::ClientInternal(QObject *parent)
    2. : QObject(parent)
    3. {
    4. pSocket = new QTcpSocket(this);
    5.  
    6. connect(pSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
    7.  
    8. pSocket->connectToHost("192.168.50.77", 1234);
    9. if (!pSocket->waitForConnected())
    10. {
    11. qDebug()<<"Unable to connect";
    12. return;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    now it seems to work!
    why?
    Last edited by naroin; 20th January 2011 at 09:46.

  16. #35
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Because you have the event loop running. Anyway, get rid of the threads, you really don't need them since socket communication is asynchronous.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #36
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    okay but why some data is dropped with my "false thread" ?
    is there a event loop for each thread of one for all the application?

  18. #37
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    Each thread can have its own event loop, but it's not automatic, you have to execute it yourself.

  19. #38
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Quote Originally Posted by naroin View Post
    okay but why some data is dropped with my "false thread" ?
    is there a event loop for each thread of one for all the application?
    Remove threads and all should be working fine. We must have missed the fact you were calling a slot of the QThread object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #39
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket readyRead strange behavior

    yes it works well without my QThread object.
    But can i have a technical explanation of WHY it didn't work well?
    I think i'm missing something important here, and i would like to understand.

    However, thanks for your help so far

  21. #40
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket readyRead strange behavior

    Quote Originally Posted by naroin View Post
    But can i have a technical explanation of WHY it didn't work well?
    Sure. Your QThread object "lives" in the main thread which means all events for it are processed by the main thread. Your socket lives in the thread controlled by the QThread object. This means the socket gets notified about incoming data by the worker thread and you try to read data from the main thread. Since there is no synchronization between threads here, data may be lost (if you read something and at the same time the other thread writes something to the socket object).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. The following user says thank you to wysota for this useful post:

    naroin (20th January 2011)

Similar Threads

  1. QtcpSocket readyRead Problem with Java Client
    By Bernie in forum Qt Programming
    Replies: 6
    Last Post: 12th February 2011, 11:59
  2. QTcpSocket and readyRead and QTimer
    By cafu in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2009, 13:36
  3. Question in readyRead(QTCPSOCKET)
    By morgana in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2008, 18:11
  4. QTcpSocket readyRead and buffer size
    By pdoria in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2008, 10:11
  5. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44

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.