linux network programming

if there is a client is connected to the server and if we remove
the cabel connecting them then how server comes to know
that at the other side client is not present.
Who is Participating?
feenixConnect With a Mentor Commented:
For TCP connections there is a timeout value, which controls when the connection is dropped. For example, if you have a timeout of 10 seconds and you remove the cable for more than that, the server gets a notice from the TCP/IP stack informing it about the disconnection.

So basically it's the same as if the client would have disconnected the connection 'gracefully'.
This would hardly be an useful solition. First, the tcp engine wouldn't start tcp timer for that connection if there is no outstanding package on it for ACK. So, if the cable was removed when the connection is in fully idle state, the server wouldn't find it even if you set the tcp timeout to 10 seconds or shorter.

Second, even if the cable was removed during non-idle state, setting tcp timeout to 10 seconds is questionable. The default tcp timeout for most tcp engine is 9 to 2 minutes (i.e 540 to 120 seconds). Setting this timer to as short as 10 seconds would cause tcp connection be pre-maturelly removed for some transient errors.  

Setting TCP_KEEPALIVE on may allow server to detact the cable failure of a completely idled connection via a low level ping. However, the keep alive timer by default is 2 hours and not all systems allow you to change it without reconfigure the kernel. An application level ping may be used alternatively but it can only find the cable failure till next ping time and has to wait for the long enough tcp timeout (i.e. 120 to 540 seconds) before report the error.
abhishekpatelAuthor Commented:
how i programmed(programing in c under linux) my server so it can comes to know that on the other side client is not present in the fully idle connection.
           basicly my server must come to know when the client
is logged in or logged out. If there is a network failure occures
in betwween them then also the server must know that network
failure and logged out the client so its total login time can be
calculated exactly.
abhishekpatelAuthor Commented:
i want my server to know the disconnection ofclient indirectly (e.g. cable break,network failure). I don't want the system telling to my server that client is not present
at the other side.
For kejin, I didn't say that you SHOULD set the timeout to that short value, I only used the 10 seconds as an example value. Of course the reasonable values are a lot longer.

And you are right for the fact that if there is no data to be sent (or just sent but waiting for an ACK), the server does not disconnect. That I forgot to mention.
All Courses

From novice to tech pro — start learning today.