Results 1 to 2 of 2

Thread: Difference between normal socket and QTcpSocket

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Difference between normal socket and QTcpSocket

    I have a program that acts as both a client and a server at the same time. It acts as a client to receive data from another program. And it acts as a server to pass that data to another program.

    So let me first describe the problem, then I will show my code.

    1) For some reason, my client side cant reconnect with my other server when the server is terminated and restarted. Or if the server is started AFTER my client. Weird..I mean..it should keep waiting for connection.

    2) I find that mInput->waitForConnected( 1000 ) doesnt wait for 1 second before attempting to connect again. It actually repeatedly tries to connect which brings the CPU load up.

    If you check the code below, you can see I'm just trying to use Qt's socket like a standard UNIX socket.

    If I'm using this wrong, please advise. Thank you!

    Here's my client code

    mInput is of QTcpSocket type. It is the client that receives data from other program.
    mServer is of QTcpServer type. mOutput is the client socket connected to this server.

    Qt Code:
    1. mServer->setMaxPendingConnections( 1 );
    2. mServer->listen( QHostAddress::Any , 8210 );
    3.  
    4. while( 1 )
    5. {
    6. mServer->waitForNewConnection( -1 );
    7. mOutput = mServer->nextPendingConnection();
    8.  
    9. mInput->connectToHost( QString("172.17.5.10"), 9001, QIODevice::ReadOnly );
    10. while( mInput->waitForConnected( 1000 ) == false )
    11. {
    12. }
    13.  
    14. while( 1 )
    15. {
    16. // Get data from server
    17. // If something goes wrong, break;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Difference between normal socket and QTcpSocket

    Quote Originally Posted by ShaChris23 View Post
    1) For some reason, my client side cant reconnect with my other server when the server is terminated and restarted. Or if the server is started AFTER my client. Weird..I mean..it should keep waiting for connection.
    Probably the system blocks the socket to prevent trashing the new connection with stale data. Are you sure your server application didn't crash? Did it close the socket properly? With BSD sockets you should set the SO_REUSEADDR option on the socket to prevent blocking the port even after a process termination. You can do that with QTcpServer as well although I'd first check if you don't kill the server without closing the server socket first.


    2) I find that mInput->waitForConnected( 1000 ) doesnt wait for 1 second before attempting to connect again. It actually repeatedly tries to connect which brings the CPU load up.
    You shouldn't put waitForConnected() in a while loop... The tcp stack will try connecting to the other host for as long as the timeout lets it to. The parameter for waitForConnected() is the timeout that prevents the application from blocking if the connection can't be established within a specified amount of time. So I'd suggest using the following:
    Qt Code:
    1. mInput->connectToHost( QString("172.17.5.10"), 9001, QIODevice::ReadOnly );
    2. if( !mInput->waitForConnected( 10000 )){ // if it can't connect within 10s, it probably won't at all
    3. qDebug() << "Can't connect to server. Bailing out";
    4. mInput->disconnectFromHost();
    5. mServer->close();
    6. exit(1); // or similar
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 21:44

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.