I wrote a program on C , where a client sent one time some information to a server. I used TCP sockets.And some time the server calculated and should to sent result to the client. How can I detect if connection on The server or the client was broken?
-
2It's simply not doable in the general sense. TCP/IP was designed for unreliable, slow connections, so it takes a long time until a communication partner decides that the other one is gone, unless they closed the connection properly. But a broken cable or triggered power switch, resulting just in missing answers, as such do not indicate a broken connection. As an aside, that was the motivation for the SSL heartbeat extension which aimed at detecting that quicker. And that's probably what you must do: Implement some kind of heartbeat. Just make sure you check the buffer lengths ;-). – Peter - Reinstate Monica May 11 '14 at 12:33
-
1This may answer your question: http://stackoverflow.com/questions/16320039/getting-disconnection-notification-using-tcp-keep-alive-on-write-blocked-socket – hyde May 11 '14 at 12:44
-
1What do you consider "dead"/"broken"? – alk May 11 '14 at 14:08
-
For example, if the client's program was finished by me before it all calculated. – user3533019 May 11 '14 at 15:01
-
2The short answer is "Follow the specification for the protocol you are implementing on top of TCP. If it doesn't specify how this is done, then it's deficient and needs to be fixed." When you write code to implement a protocol, you follow the protocol's specification. If the protocol is broken or incompletely specified, that's not a C coding issue. – David Schwartz May 12 '14 at 04:30
4 Answers
You may want to try TCP keepalives.
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9`
In the above example, TCP keep-alive timer kicks in after the idle time of 7200 seconds. If the keep-alive messages are unsuccessful then they are retried at the interval of 75 seconds. After 9 successive retry failure, the connection will be brought down.
The keepalive time can be modified at boot time by placing startup script at /etc/init.d.

- 1,862
- 1
- 15
- 21
-
So but how can I know that connection was brought down? And does way exist to change TCP keep-alive parameters directly in program by system call? – user3533019 May 11 '14 at 14:48
-
@user3533019 If TCP keepalive detects a broken connection it will cause `ECONNRESET` when you next do a send or receive on it. – user207421 May 12 '14 at 04:58
There is a way to detect, on Linux, dead sockets without reading or writing to them:
- Get the numeric (
uint
) file descriptor from the socket handler. readlink
the file/proc/[pid]/fd/[#hander]
. If it's a socket it will return a string likesocket:[#inode]
.- Read
/proc/net/tcp
, search for the line with that inode (11th column). - Read the status (
st
) column on that line (4th column). If it's 0x07 (Close) or 0x08 (TIME_WAIT), the socket is dead.

- 621
- 6
- 15
The only way I know for a program to know for certain that a TCP connection is dead is to attempt to send something on it. The attempt will either time-out or return with an error condition. Thus, the program doesn't need to do anything special -- just send the stuff it was designed to send. It does need to handle, however, all possible error-conditions. On time-out, it could retry for a limited time or decide that the connection is dead. The latter case is appropriate if sending the same data multiple times would be harmful. After this or an error condition, the program should close the current connection and, if appropriate, re-establish it.

- 7,702
- 5
- 33
- 59
-
It is good way, but it requires to allocate a new thread in the client and in the server , which does it. I can do it, but it takes a lot of resources. And my program should be high-performance. Or does it exist another way to realize your idea? – user3533019 May 11 '14 at 17:09
-
1@user3533019 The same thread can be used in the client program. The server program will have to create a new thread and, probably, close the connection in the previous thread. The time to do this will be insignificant compared to the time spent by the client in the timeout. – Steve Emmerson May 11 '14 at 22:33
TCP Keep-Alive is a reliable way to determine if the peer is dead.That is if the peer application exited without doing a proper closure of the open TCP connection.
http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
Lookout for how to enable tcp keep-alives(SO_KEEPALIVE) per socket using setsockopt
call.
Another method is that the client and the server application agree on heartbeats at regular intervals. Non arrival of heartbeats should indicate that the peer is dead.

- 3,443
- 15
- 26
-
I found them to be very unreliable actually, I can't seem to get them to work. I think the best way of doing this is to implement a heartbeat mechanism. – Epic Speedy Oct 05 '22 at 20:23