PDA

View Full Version : Difference between normal socket and QTcpSocket



ShaChris23
16th June 2007, 04:14
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.



mServer->setMaxPendingConnections( 1 );
mServer->listen( QHostAddress::Any , 8210 );

while( 1 )
{
mServer->waitForNewConnection( -1 );
mOutput = mServer->nextPendingConnection();

mInput->connectToHost( QString("172.17.5.10"), 9001, QIODevice::ReadOnly );
while( mInput->waitForConnected( 1000 ) == false )
{
}

while( 1 )
{
// Get data from server
// If something goes wrong, break;
}
}

wysota
16th June 2007, 04:38
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:

mInput->connectToHost( QString("172.17.5.10"), 9001, QIODevice::ReadOnly );
if( !mInput->waitForConnected( 10000 )){ // if it can't connect within 10s, it probably won't at all
qDebug() << "Can't connect to server. Bailing out";
mInput->disconnectFromHost();
mServer->close();
exit(1); // or similar
}