Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    [SOLVED] select / TCP keep-alives


    Have a question regarding the implementation of TCP keep alives.

    If I enable the option of keep-alives on a TCP socket, is there any way that my application can get the response to the keep-alive back from the kernel. Basically, I have a 'select' blocking on a socket on which I have enabled keep-alives. I need to know if select would unblock if a response to a keep-alive sent by the kernel is received on the socket.

    Many thanks.

  2. #2

    One more question

    Does select unblock if the other end of a socket is closed i.e. a server in select is un-blocked if the client closes the socket at its end?


  3. #3
    First of all sorry about my previous post. I tried it out and got the answer:

    "Select unblocks on a closed socket!"

    Secondly, let me specify more details about the question asked earlier.(fresh from the tips from the newbie guide):

    My purpose is to log keep-alive messages for an application written in C. Instead of creating a new message-type to exchange the keep-alives, I planned on using the in-built keep-alive mechanism for doing this. Since my application blocks on a select, I need to find a way that allows me to get the keep-alive message back up through select so that I can log it. I could not find any information on how this can be accomplished on the web or through man pages as they only talk about setting up this feature in Linux and do not discuss the implications. Also, in the code, the select does not unblock - tested using a 'sleep' for a time greater than the keepalive time configured, in the client code with the server blocked on a select.

    My question is thus:

    Please post your suggestions on the same. Please let me know what I need to do to allow the keep-alive to be available for reading via select.

    Many thanks.

  4. $spacer_open
  5. #4

    Found a solution

    Unless one wants to hack the kernel, there is no way of getting the keep-alive back to the user. However, I found a work around that seems to work pretty well for me.

    0. Enable the keep-alive messages on the connected socket at the end expecting keep-alives.
    1. Start a timer to send the keep-alives at one end (client) of the connection.
    2. Upon timer expiry send a keep-alive to the other end of a connected socket.
    3. The end where you enabled the keep-alives on the socket will restart the required timers whenever a message is received. (Default behavior offered by the kernel)
    4. Log the keep-alives received. (server)
    5. In case the worker end exits, the timer for keep alives expires telling the server that the other end is dead. (Required so that select can monitor other 'up' sockets)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts