Detecting socket timeout?

When I have a SOCK_STREAM socket connection established (TCP/IP), is there a
way to detect a loss of communication without using application-level
heartbeats? For example, if someone unplugs the ethernet cable can I detect
this and close the socket? In the past, I have used application level
heartbeats (dummy messages) at a specified interval. If the interval
expires, I deem the connection lost and close the socket. Now I’m
connecting to a piece of equipment that does not support application
heartbeats. Any suggestions?

Thanks,
Chris Southern

You either have to send you own keepalive message, or, I have read but have no
experience in, you have to use the select() function to listen to the port for
data or problems.

Check out Unix Network Programming: Networking APIs: Sockets and XTI by W.
Richard Stevens. Most experienced sockets programmers that I know recommend it
as the bible of socket programming.

Chris Southern wrote:

When I have a SOCK_STREAM socket connection established (TCP/IP), is there a
way to detect a loss of communication without using application-level
heartbeats? For example, if someone unplugs the ethernet cable can I detect
this and close the socket? In the past, I have used application level
heartbeats (dummy messages) at a specified interval. If the interval
expires, I deem the connection lost and close the socket. Now I’m
connecting to a piece of equipment that does not support application
heartbeats. Any suggestions?

Thanks,
Chris Southern

Chris Southern <csouthern@sprintmail.com> wrote:

When I have a SOCK_STREAM socket connection established (TCP/IP), is there a
way to detect a loss of communication without using application-level
heartbeats? For example, if someone unplugs the ethernet cable can I detect
this and close the socket? In the past, I have used application level
heartbeats (dummy messages) at a specified interval. If the interval
expires, I deem the connection lost and close the socket. Now I’m
connecting to a piece of equipment that does not support application
heartbeats. Any suggestions?

Chech the document for SO_KEEPALIVE/TCP_KEEPALIVE. The first set the
socket option so that it will check the connection after a period of
idle time (default 2 hour), the second used to change that 2 hour.

-xiaodan

Thanks, Xiaodan

I’ve seen the SO_KEEPALIVE in socket.h and in the documentation. Two hours
is a little long for our application. The TCP_KEEPALIVE is new to me. Is
there specific documentation that you have seen?


Chris