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);

    if(!myInfoSocket.waitForConnected())
    {
    myInfoSocket.write((char*)&myCmd,sizeof(myCmd));//myCmd is just a int type.

    if(!myInfoSocket.waitForBytesWritten())
    {
       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;
    }   
    
    myInfoSocket.read(pTempBuffer,sizeof(int)*2);
    

    }@



  • You should not rely on @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.



  • 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



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


  • Moderators

    [quote author="henryxuv" date="1345430422"]I have read the example's code about network program. It is too simple. Can anybody help me? thanks[/quote]
    For sure someone is willing to help you. However, it would be helpful describing what exactly your problem is.

    [quote author="liuyanghejerry" date="1345105001"]You should not rely on @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.[/quote]
    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.



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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.