0

From my understanding of a python socket, there is a read end and a write end. I also understand that you can close one half of the socket (on some systems). Furthermore, you can check in python if a socket has closed by sending something and checking for an exception.

I was wondering if there was a way to "reliably" check if a server has closed the "read" end of the socket, AFTER the client has closed its "write" end of the socket.

For example, if a server were running, and this is the client code:

sock.shutdown(socket.SHUT_WR)
#I want to check if I can still read from the socket at this point in code.

Is this possible?

Bach
  • 6,145
  • 7
  • 36
  • 61
mdenton8
  • 619
  • 5
  • 13
  • 1
    May be that question http://stackoverflow.com/questions/2441928/testing-for-a-closed-socket will help you. You could check the same way in python:sock.recv(1, socket.MSG_DONTWAIT | socket.MSG_PEEK) – psl Apr 22 '14 at 09:08

1 Answers1

2

From my understanding of a python socket, there is a read end and a write end.

No. Sockets are full-duplex, bidirectional, read/write, ...

I also understand that you can close one half of the socket (on some systems).

You can close either for reading or for writing.

Furthermore, you can check in python if a socket has closed by sending something and checking for an exception.

You can check if a connection has been closed by the peer by sending something etc.

I was wondering if there was a way to "reliably" check if a server has closed the "read" end of the socket, AFTER the client has closed its "write" end of the socket.

If you're the server, you don't need to check, because you're the one that did the close. If you're the client, you can't send anything, so you can't check.

For example, if a server were running, and this is the client code:

sock.shutdown(socket.SHUT_WR)

I want to check if I can still read from the socket at this point in code.

The only way to tell whether you can still read from the socket is to try to read from the socket. If the peer has closed the socket, or half-closed it for sending, you will get end of stream, however that manifests itself in your language. This isn't what you were asking about above: there you were asking about the server peer closing its socket for reading.

user207421
  • 305,947
  • 44
  • 307
  • 483