2

I wrote a TCP server using Python Twisted to send/receive binary data from clients.

When a client close their application or calls the abortConnection method, I get the connectionLost event normally but when the client disconnects unexpectedly, I don't get the disconnect event, therefore, I can't remove the disconnected client from the queue.

By unexpected disconnect I mean disabling the network adapter or lost the network connection somehow.

My question is, how can I handle this sort of unexpected connection losts?

2 Answers 2

3

Enabling TCP keepalive on your socket should fix this.

By default this is disabled and thus if the connection is broken client and server will never find out the connection is dead and simply assume there is no packets being send. A keepalive will send a 'heartbeat' each x interval to see if the connection is still alive.

Read more and check how to do it here: How to use TCP Keepalive with Endpoints in Twisted?

Sign up to request clarification or add additional context in comments.

2 Comments

This is great thank you. But the problem is that I'm using Twisted and I think we can't change the keepalive interval in Twisted (no documentation at least).
Damn, I didn't expect that. Then I'm afraid you will have to create your own heartbeat by sending a packet to the client and waiting for the response.
0

The only way to support a cross-platform unexpected disconnection (unplug) is to implement a application-level ping message to ping clients in a specific interval.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.