[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How To Tell If a Socket Has Been Closed




If you're expecting to receive data consistently from the remote host, 
you can use setSoTimeout().

If not, you'll probably have to use one of the other methods described 
by Brad.

If you do find something else, please share, b/c I'm looking to employ 
something like this in the near future.

Calvin

Gregory A. Lusk wrote:
> My initial thought has been to try and retrieve the handle of the socket 
> (if it’s a Windows environment), and then call the Winsock API select() 
> function that will return the status of the socket.  But short of 
> creating my own socket class that calls the API directly, I’m not sure 
> how to do it. 
> 
>  
> 
> Or perhaps there is a way to get it from the VM?
> 
>  
> 
>  
> 
> Greg Lusk
> 
> -----Original Message-----
> *From:* Bradley Roberts [mailto:Bradley@Roberts.net]
> *Sent**:* Friday, December 27, 2002 9:52 AM
> *To:* Gregory A. Lusk
> *Subject:* RE: How To Tell If a Socket Has Been Closed
> 
>  
> 
> That's a tough issue, since the remote host's behavior is unpredictable. 
> I can think of two approaches to this issue that I've seen in practice:
> 
> 1. You use another thread to "ping" the remote host from time to time to 
> determine if it is still connected. If the ping fails, then you set a 
> flag that indicates that the connection has been terminated on the other 
> end. By checking this flag before you send a packet, you'll know whether 
> to proceed or throw an exception immediately.
> 
> 2. You just send every packet in the hope that you'll get a response. 
> When the host fails to respond, you timeout on the call, and throw an 
> exception indicating the failed response. Then you use that exception 
> handling to set a global flag that indicates that the host has 
> disconnected. You then stop trying for some pre-determined time period. 
> When your time period ends, you try again for the next packet. If you 
> then get a response, you reset the flag and go back to a normal process. 
> If the retry fails, you just repeat the time period again.
> 
>  
> 
> However, you can always wind up with non-deterministic behavior if your 
> application tries to send a packet at the wrong time. The packet will go 
> out the door, but you won't get an acknowledgement back. You may then 
> have to simply wait for some timeout to fire before concluding that the 
> remote host disconnected on you.
> 
>  
> 
> Hope this helps.
> 
>  
> 
> Sincerely,
> 
> Brad Roberts
> 
> EM:    Bradley@Roberts.net <mailto:Bradley@Roberts.net>
> 
>  
> 
>  
> 
>  
> 
>     -----Original Message-----
>     *From:* Gregory A. Lusk [mailto:glusk@mesasub.com]
>     *Sent:* Friday, December 27, 2002 9:30 AM
>     *To:* ajug-members@www.ajug.org
>     *Subject:* How To Tell If a Socket Has Been Closed
> 
>     Can anyone recommend a method for determining if a socket has been
>     closed by a remote host?  The scenario is that a thread is listening
>     on a ServerSocket, blocking on its accept() method.  A remote host
>     connects and the accept() method unblocks, returning the resulting
>     socket.  Later, the remote host simply disconnects.  I need to
>     detect this disconnection at the remote end.
> 
>      
> 
>     Thanks,
> 
>      
> 
>     Greg Lusk
> 
>      
>