August 16, 2012

henryxuv henryxuv
Lab Rat
44 posts

A question about QTcpSocket

 

I am writing a c style dynamic library using QTcpSocket. but my code not always work well. below is the sequence that I called.
My server has been verified strong and correct by another dynamic library. that library call win32 function, not QTcpSocket.
Thanks for reply.

  1. myInfoSocket.connectToHost(ipAddr, iPort);
  2.  
  3. if(!myInfoSocket.waitForConnected())
  4. {
  5.     myInfoSocket.write((char*)&myCmd,sizeof(myCmd));//myCmd is just a int type.
  6.  
  7.     if(!myInfoSocket.waitForBytesWritten())
  8.     {
  9.        QMessageBox::information(NULL, "Write", myInfoSocket.errorString());
  10.        return -1;            
  11.     }
  12.  
  13.  
  14.     if(!myInfoSocket.waitForReadyRead())//here is my problem, sometime it works fine. sometimes over time.
  15.     {
  16.        QMessageBox::information(NULL, "Read", myInfoSocket.errorString());
  17.        return -1;
  18.     }  
  19.  
  20.     myInfoSocket.read(pTempBuffer,sizeof(int)*2);
  21.  
  22. }

6 replies

August 16, 2012

liuyanghejerry liuyanghejer..
Ant Farmer
40 posts

You should not rely on

  1. waitForXXX
functions. They are set to have only 3000 ms to block, by default. After that ,they’ll return false.

QTcpSocket API is design mainly for asynchronous. Please check out document for details.

August 17, 2012

henryxuv henryxuv
Lab Rat
44 posts

bool QAbstractSocket::waitForReadyRead ( int msecs = 30000 ) the waiting time is 30000 not 3000ms. It’s a long enough time for my server to respond. So I don’t think my problem is waiting too short. Maybe some function I neglect or called in wrong place, I don’t know.
Whatever, Thank you

August 20, 2012

henryxuv henryxuv
Lab Rat
44 posts

I have read the example’s code about network program. It is too simple. Can anybody help me? thanks

August 20, 2012

koahnig koahnig
Mad Scientist
2106 posts

henryxuv wrote:
I have read the example’s code about network program. It is too simple. Can anybody help me? thanks

For sure someone is willing to help you. However, it would be helpful describing what exactly your problem is.

liuyanghejerry wrote:
You should not rely on
  1. waitForXXX
functions. They are set to have only 3000 ms to block, by default. After that ,they’ll return false.

QTcpSocket API is design mainly for asynchronous. Please check out document for details.


This is a response to what and where you assumed your problem, but according to you the 30 seconds should be plenty of time to respond. Increase the number to 60, 120, or maybe 300 seconds and see, if it is solving your problem.

From the design point of view this code section is just opening the connection and reading or writing. So presumably you will open the connection on the next turn again. That is not the typical strategy.

Check-out the fortune client /server example. You can start both on your machine and go through it with the debugger. That will help you to understand the scheme.

August 21, 2012

liuyanghejerry liuyanghejer..
Ant Farmer
40 posts

Sorry for that time thing. But sometimes, your network may really eats that time.
To be honest, there’re too many possibilities around your problem. At least you can write your error code or error string here?

August 21, 2012

henryxuv henryxuv
Lab Rat
44 posts

Are you Chinese? If you don’t mind , please give me a E-mail, or I can ask you from Skype, MSN,etc…,My email and MSN is henryxuv@hotmail.com

 
  ‹‹ QStandardItemModel/QtreeView fast setting background of items      Box-shadow in qss? ››

You must log in to post a reply. Not a member yet? Register here!