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