-
November 10th, 2023, 07:24 AM
#1
network down with socket effects
I'm writing network app client/server with tcp persistent connection. If network went down, both client and server socket are kept opened, they are not closed automatically. So how can I prevent this behaviour ? By keepalive mechanism in C ?
What do you suggest me ?
-
November 11th, 2023, 03:35 AM
#2
Re: network down with socket effects
https://stackoverflow.com/questions/...at-the-other-e
https://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/
In terms of your use of send()/recv(), nothing changes.
At some point, these functions will return an error status indicating that the connection is no longer available.
It depends how chatty your client/server are.
If you're sending large amounts of data back and forth at intervals of seconds, then keepalive isn't going to do anything for you. Buffers are going to fill up, and the lowest levels will soon notice there is nothing at the other end of the connection.
If you're sending small amounts of data at intervals of hours, then keepalive may help - in the sense that you find out sooner that the connection is dead.
Or it may make it worse.
-
November 11th, 2023, 10:23 AM
#3
Re: network down with socket effects
 Originally Posted by salem_c
In terms of your use of send()/recv(), nothing changes.
At some point, these functions will return an error status indicating that the connection is no longer available.
Ok thank you.
I thought this behaviour of KeepAlive could to be useful only to prevent some application states before send/recv was used. what do you think ?
-
November 11th, 2023, 10:52 AM
#4
Re: network down with socket effects
If you only sent one message an hour, it might be several hours before you notice (without keepalive)
With keepalive, you get an answer sooner, because the OS is monitoring the link health all the time.
Either way, your send()/recv() fail in the same way.
-
November 12th, 2023, 05:53 AM
#5
Re: network down with socket effects
In std C89 can you suggest me where to sei KeepAlive ?
In what primitive? Socket ? send ? recv ? And about timing value ?
-
November 12th, 2023, 07:07 AM
#6
Re: network down with socket effects
Before you start messing with keepalive, does your application actually work without it?
Make sure you understand this
https://tldp.org/HOWTO/html_single/T...-HOWTO/#whyuse
You enable it (or disable it) using
https://tldp.org/HOWTO/html_single/T...TO/#setsockopt
When you do it seems to be up to you.
If you're dead set on using it no matter what, then just after the connect/accept would seem like a good place.
-
April 25th, 2024, 11:58 AM
#7
Re: network down with socket effects
To handle network disconnections in your TCP client/server application, enable the TCP keepalive mechanism. This can be done by setting the SO_KEEPALIVE option on your sockets, which helps detect and close broken connections by sending periodic probes. Adjust additional TCP keepalive settings as needed to fit your application's responsiveness requirements.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|