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.
- myInfoSocket.connectToHost(ipAddr, iPort);
- myInfoSocket.write((char*)&myCmd,sizeof(myCmd));//myCmd is just a int type.
- QMessageBox::information(NULL, "Write", myInfoSocket.errorString());
- return -1;
- if(!myInfoSocket.waitForReadyRead())//here is my problem, sometime it works fine. sometimes over time.
- QMessageBox::information(NULL, "Read", myInfoSocket.errorString());
- return -1;
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.
You should not rely onfunctions. 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.