Detecting a broken socket

Having established a TCP/IP socket with a server, what is the best way of
determining if the connection has been lost (broken cable, server crash,
etc.)? Data is only being written to the socket by the client and there is
no feedback from the server via confirmation messages or the like.

Similarly, how about the situation where a server is sending data to a
client. Can the client detect the connection has been broken? (yes I know
the data will stop, but it may do that in the course of normal operation so
it’s not a good indicator)

TIA

According to standards, when the server disconnects, the client should receive
an error “Connection reset by peer”. Sorry, I don’t remember the error number.

Gregory Fedor wrote:

Having established a TCP/IP socket with a server, what is the best way of
determining if the connection has been lost (broken cable, server crash,
etc.)? Data is only being written to the socket by the client and there is
no feedback from the server via confirmation messages or the like.

Similarly, how about the situation where a server is sending data to a
client. Can the client detect the connection has been broken? (yes I know
the data will stop, but it may do that in the course of normal operation so
it’s not a good indicator)

TIA