[Qt-interest] QTcpSocket suddenly disconnects

Joshua Senecal jgsenecal at yahoo.com
Wed Aug 5 05:20:29 CEST 2009


Okay, I've traced through the code somewhat and here's what I've come up 
with thus far.

If I terminate the server while the client is connected, the client 
immediately enters QAbstractSocketPrivate::readFromSocket() (why?). 
Nothing is read obviously, but toward the end of the procedure is this 
block of code, the body of which is taken:

if (!socketEngine->isValid()) {
    socketError = socketEngine->error();
    q->setErrorString(socketEngine->errorString());
    emit q->error(socketError);
#if defined(QABSTRACTSOCKET_DEBUG)
    qDebug("QAbstractSocketPrivate::readFromSocket() read failed: %s",
           q->errorString().toLatin1().constData());
#endif
    resetSocketLayer();
    return false;
}

When execution reaches this line:

emit q->error(socketError);

execution immediately goes to a switch statement in my class' 
moc-generated code that handles signals. My socket which initiates a 
reconnect is called. The reconnect completes, but then code execution 
returns from the call to emit q->error(socketError) and continues. The 
next line is resetSocketLayer(). When this line executes, the server 
socket sees a disconnection from the client side. The client likewise 
disconnects. 

So, I think what's happening in a nutshell is the code that executes as 
a result of the original server disconnection is not given a chance to 
execute completely before my reconnect routines begin execution. I'll 
have to see what I can do to remedy this.

-Josh



More information about the Qt-interest-old mailing list