CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2018
    Posts
    165

    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 ?

  2. #2
    Join Date
    Nov 2018
    Posts
    154

    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.

  3. #3
    Join Date
    May 2018
    Posts
    165

    Re: network down with socket effects

    Quote Originally Posted by salem_c View Post

    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 ?

  4. #4
    Join Date
    Nov 2018
    Posts
    154

    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.

  5. #5
    Join Date
    May 2018
    Posts
    165

    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 ?

  6. #6
    Join Date
    Nov 2018
    Posts
    154

    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.

  7. #7
    Join Date
    Apr 2024
    Posts
    1

    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
  •  





Click Here to Expand Forum to Full Width

Featured