Thread: Detecting socket closed

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    Detecting socket closed

    This may be a super simple question, but for the life of me I can't find any sources to answer it on Google or I've read something that confused me.

    I'm communicating with a server that requires I send it a handle name for my connection, depending on some rules on handle formatting it either accepts/rejects me. The thing is that it rejects me WITHOUT notifying that it's ending the connection.

    My question is...is there any way I can detect that the socket has closed? I read a forum that said I can use descr() to feed in the file discriptor but other sources are saying that there is effectively no way of knowing if the socket is still open or not - which doesn't make sense to me (seems like that can't possibly be true).

    Thanks in advance.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by redcameron View Post
    I read a forum that said I can use descr() to feed in the file discriptor but other sources are saying that there is effectively no way of knowing if the socket is still open or not - which doesn't make sense to me (seems like that can't possibly be true).
    Don't think of a connection as a tie between client and server (that if a connection is cut, both parties would know). A connection is an agreement between client and server that they will communicate. They initiate by sending specialized packets, and then either party can send data to the other. The connection is considered closed if one party sends a specialized packet to indicate that party will no longer participate in that connection.

    If you want to check if a connection is closed, you could try sending more data, and error on timeout. I believe that is a most common way, if the server never sends the close-connection (or is never received). Most socket API should provide a way to implement this. What system/platform, etc. are you working on?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Hi there,

    Thanks for that lucid explanation :-) I'm writing for Unix based systems in standard C.

    Without repeating myself, here's another thread I created yesterday for another issue I had and managed to resolved quickly: Multiplexing read socket and stdin

    More than half way through I end up posting a simplified version of my program, so you can look at that directly.

    Basically you're saying that I can apply error checking on my write statement, if a certain value is returned I can assume the socket is closed then end my connection.

    Correct?

    Thanks!!!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by redcameron View Post
    Basically you're saying that I can apply error checking on my write statement, if a certain value is returned I can assume the socket is closed then end my connection.

    Correct?

    Thanks!!!
    Well, you can't really assume anything other than you got a timeout. That doesn't necessarily mean the other end closed the connection (at any rate, you should get a RST unless the other end is doing something stupid) -- it just means your packet never got there and/or was never acknowledged. That could be due to anything, like Godzilla eating the Internet for instance
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Heh, evil godzilla.

    So basically...I'm probably so dumb that I need this explicitly stated. I can't assume that the server closed the connection but what you're getting at is, I'm safe if I just end the connection of the write doesn't work?

    lol...

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    It's a fairly safe assumption that the connection was lost during a timeout. You could try to re-establish the connection after a timeout.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    So I'm not clear yet...when you say timeout to mean a literal timeout what I would have set in my select statement? I don't have this set for a specific reason, to force my application to block indefinitely (which IS an intended effect).

    What do YOU mean by timeout?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by redcameron View Post
    So I'm not clear yet...when you say timeout to mean a literal timeout what I would have set in my select statement? I don't have this set for a specific reason, to force my application to block indefinitely (which IS an intended effect).

    What do YOU mean by timeout?
    TCP itself has a configurable timeout. I think that's what we're talking about.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Why don't you just have your server send some response back upon a rejection? As it appears you have already gone through the TCP three way handshake or else how would the server not read your handle you are sending. Are you sure that is even occuring?

    At what point in the TCP session are you breaking? For instance...

    Overview of a TCP communications session

    You may already know if this gem!

    Overview of a TCP communications session


    The question is if your server does not respond at all then you are solely relying on how TCP is to detect a closed session and that mainly will fall back on your defined timeouts as already noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. How to find out if a socket is closed or not
    By Da-Nuka in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-01-2005, 11:32 AM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM