[Qt-interest] QTcpSocket suddenly disconnects

Joshua Senecal jgsenecal at yahoo.com
Thu Jul 30 03:18:01 CEST 2009


Hi All,

I'm working on a wrapper to the QTcpSocket class, to add some additional 
functionality. In particular, I'm adding functionality such that if the 
socket is suddenly disconnected for whatever reason it automatically 
makes a few attempts to reconnect. Only if the reconnect attempts fail 
does it emit an error signal.

I've encountered some unexplained behavior, and I'm unable to pin down 
the cause. Simply put, if a client socket connects to a server, and the 
server disconnects suddenly, the client attempts a reconnect, as it 
should. However, once a reconnection is completed, the client suddenly 
disconnects.

I have slots connected to both the stateChanged and error signals. 
Here's the client debug output, based on calls to the slots:

State: Host Lookup.
State: Connecting.
State: Connected.
Client: handleConnected()

Client: sending client greeting
Client: received server greeting
Client: sending local UDP port  
Client: received remote UDP port  
Client: Sending SOCKET_ALIVE over UDP
Client: received SOCKET_ALIVE over UDP
Client socket connected.

...small data exchange here...
...I terminate and restart the server....

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
handleSocketError(): Error detected
QAbstractSocket::RemoteHostClosedError
State: Closing.
State: Unconnected.
Attempting to reconnect to host.
Waiting for a reconnect....
State: Host Lookup.
State: Connecting.
State: Connected.
Client: handleConnected()

Client: sending client greeting
Client: received server greeting
Client: sending local UDP port  
Client: received remote UDP port  
Client: Sending SOCKET_ALIVE over UDP
Client: received SOCKET_ALIVE over UDP
Client socket connected.
State: Closing.
State: Unconnected.


The connected server socket reports an error: the client disconnected.

Any suggestions as to what may be causing this? It's almost like there's 
a phantom close() or similar floating around somewhere. I've checked the 
code and I didn't see any out-of-place calls that would close the 
socket. 

Thanks!

-Josh Senecal



More information about the Qt-interest-old mailing list