Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A question about QTcpSocket
Qt 6.11 is out! See what's new in the release blog

A question about QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    henryxuv
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      liuyanghejerry
      wrote on last edited by
      #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.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        henryxuv
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • H Offline
          H Offline
          henryxuv
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            [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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              liuyanghejerry
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                henryxuv
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved