Results 1 to 8 of 8

Thread: bug in Qt socket

  1. #1
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default bug in Qt socket

    I have two apps. one receive data with TCP socket , another generate the input. they both run the timer of 30 millisecond. receiver has to process the data that comes in. Reading and processing are in same thread. after few second receiver start to show that cannot keep up with input.(it could be a bug, or it could be that extra process take time) After 10 mins the difference is huge. for example: Receiver get package 1000, but input is package 5000. I believe all the data accumulated in lower level real socket buffer. The funy thing is that when I stop the input, the receiver stop getting data as well. this is not desired behavior. it should keep reading the data in the socket. In fact, readyRead () is not emitting any more.

    Is this a bug? or i use it wrong?

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: bug in Qt socket

    give minimal exaple code

  3. #3
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: bug in Qt socket

    Now I slow the input at 500 milisocond. there still are the big differences after while. what the hell going on with this QTcpSocket!!!!!!!!!!!!!!!!!!!!

  4. #4
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: bug in Qt socket

    Qt Code:
    1. void ClientSocket::tcpReady(){
    2.  
    3. QDataStream stream( &socket );
    4. if(dataSize==0){
    5.  
    6. if(socket.bytesAvailable() < (int)sizeof(quint16)){
    7. return;
    8. }
    9. stream >> dataSize;
    10. }
    11.  
    12. if(dataSize <= socket.bytesAvailable()){
    13. quint16 type;
    14. qint32 frame;
    15. qint32 x;
    16. qint32 y;
    17. QTime sendTime;
    18. quint16 numOfMsg;
    19. quint16 dir;
    20. qint32 tankId;
    21. bool dead;
    22.  
    23. stream>> numOfMsg;
    24.  
    25.  
    26. for(int i=0;i<numOfMsg;i++){
    27. stream>> type;
    28.  
    29. switch (type){
    30. case PLAYER:
    31. stream >>frame >>tankId>>x >>y >>dead>>sendTime;
    32. //if(!checkPrediction(frame,x,y,dead)){
    33. cannonPrt->updateTank(frame,tankId,x,y,dead,sendTime);
    34. //}
    35. qWarning()<< "type "<< type<<"frame: "<<frame<<"x: "<< x<< "y: "<<y <<"time: " << sendTime.toString("m:s:z");
    36. break;
    37. case BULLET:
    38. stream >>frame >>x >>y >>sendTime>>dir;
    39. qWarning()<< "type "<< type<<"frame: "<<frame<<"x: "<< x<< "y: "<<y <<"time: " << sendTime.toString("m:s:z") <<"dir "<<dir;
    40. break;
    41. case INITIAL:
    42.  
    43. stream>>frame >>x>>y>>tankId>>sendTime;
    44. cannonPrt->setFrame(frame);
    45. cannonPrt->addTank(true,tankId,x,y);
    46. mainTankId=tankId;
    47. label->hide();
    48. break;
    49. case CHECK:
    50. stream >>frame>>sendTime;
    51. if(cannonPrt->getFrame()-frame>20 || cannonPrt->getFrame()-frame<-20){
    52. cannonPrt->setFrame(frame);
    53. }
    54. break;
    55.  
    56. default:
    57. break;
    58. }
    59.  
    60. }//end for loop
    61.  
    62.  
    63. dataSize=0;
    64. }
    65.  
    66. }
    To copy to clipboard, switch view to plain text mode 


    this is input


    Qt Code:
    1. void ClientThread::sendMessage(){
    2. #ifdef _DEBUG
    3. qWarning("sendMessage %d\n", QThread::currentThreadId());
    4. #endif
    5. numOfMsg++;
    6. if(numOfMsg>CHECK_PERIOD){
    7. sendCheckMessage();
    8. numOfMsg=0;
    9. }
    10. QList<QByteArray> dataToSend= cannonPtr->getDataToSend();
    11. if(!dataToSend.isEmpty()){
    12. QByteArray data;
    13. QDataStream out( &data, QIODevice::ReadWrite );
    14. out.setVersion(QDataStream::Qt_4_4);
    15. out << (quint16)0;//reserve for size
    16. out << (quint16)dataToSend.count();
    17. for(int i=0; i<dataToSend.count(); i++){
    18.  
    19. data.append(dataToSend[i]);
    20. //out<<dataToSend[i];
    21.  
    22. }
    23. out.device()->seek(0);
    24. out<< (quint16)(data.size() - sizeof(quint16));
    25. #ifdef _DEBUG
    26. quint16 size;
    27. quint16 num;
    28. quint16 kind;
    29. qint32 frame;
    30. qint32 id;
    31. qint32 x;
    32. qint32 y;
    33. bool dead;
    34. QTime time;
    35. out.device()->seek(0);
    36. out>>size>>num>>kind>>frame>>id>>x>>y>>dead>>time;
    37. qWarning()<<"size: "<<size<<"num: "<<num<<"frame: "<<frame;
    38. #endif
    39. socketPrt->write(data);
    40. socketPrt->waitForBytesWritten();
    41.  
    42. dataToSend.clear();
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: bug in Qt socket

    now I even stop the data process, so the whole thread is sitting there doing nothing but to read the incoming data and I set incoming rate to 500 millisecond(very slow rate). after while I still can see input send package 100 and receiver get package 80.. this is unbelievable~~~

  6. #6
    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: bug in Qt socket

    I'd say your code is invalid. Aren't you experiencing any missing data anywhere "in the middle" of the transmission? I would really avoid creating a new data stream each time the socket emits a readyRead() signal.
    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.


  7. #7
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: bug in Qt socket

    I would really avoid creating a new data stream each time the socket emits a readyRead() signal.
    thanks for suggestion, but what is wrong with create the new data stream every time?

    data are perfectly deleveried

  8. #8
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: bug in Qt socket

    I think your problem is that you assume that your message is delivered as you send it.

    Due to network traffic ,sometimes it will decide to split your messages into sub packets then in this case your client's recieve method is screwed.

    Hint:
    In every readyRead signal I would check if my previous numMessage is satisfied. or Put an end of transmission trigger so your client can check it if you have the full message.

    Some more thoughts:
    I don't think your loop using operator << will wait for another packet to arrive.

    To prove my point try sendin a small message like 3 bytes @ a time. and maybe your network will deliver the full message everytime.

    my 2 cents,

    baray98

Similar Threads

  1. socket notifiers cannot be enabled from another thread
    By babu198649 in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2009, 15:15
  2. Greenphone mini USB socket problem
    By izico in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 25th September 2007, 11:59
  3. Socket bind error
    By rburge in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 00:18
  4. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52
  5. qt socket question
    By bluesguy82 in forum Qt Programming
    Replies: 4
    Last Post: 29th August 2006, 15:42

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.