Does Select.select () not catch exception conditions on a socket?

Python 2.7, Windows XP. I have a server that sends messages to the client (s). I use the select module to check for socket readiness, and also to rule out exceptional conditions. I had the impression that if the client closed the socket, select () would return the specified socket to the socket list of exceptional conditions, but it does not seem to do this:

lin, lout, lex = select.select(socklist, socklist, socklist) for sock in lin: # handle incoming messages for sock in lout: # send updates for sock in lex: # shut down server-side objects for particular client 

What would be the best way for the server to determine if the client is still connected? The server does not always send data, so I do not need to rely on socket.send () to check if the client remains there.

+4
source share
3 answers

A closed socket is no exception (error). What will happen since the socket will be on the read list (lin), and when you read it, you will get 0 bytes. This means that the other end has closed the nest.

Update

In normal practice, you will never see anything in the exception list and you can safely ignore it. This is for rarely used things like out-of-band (OOB), etc.

Answer the question about updates :

Reliably and quickly detecting that the other end of the socket is gone can be difficult. If it is important that this is reliable, always and on time, you should use a higher level mechanism such as keepalive / heartbeat.

If the client performs a clean shutdown of the socket, you should see the socket in the read list. Reading from the socket will return 0 bytes, indicating that the socket is closed (EOF).

+6
source

The exact definition of "exceptional conditions" on the socket depends on the underlying implementation. For Windows XP, the main implementation is the WinSock selection function , and the exceptional conditions are:

  • While processing a connection call (non-blocking), the connection attempt failed.
  • OOB data is readable.
+2
source

The client closing the socket is not exclusive. This is actually nothing but exceptional. You will get various errors in the list of exceptional conditions, but a properly closed socket is not one of them.

+1
source

Source: https://habr.com/ru/post/1345625/


All Articles