[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: How To Tell If a Socket Has Been Closed
- To: <ajug-members@www.ajug.org>
- Subject: RE: How To Tell If a Socket Has Been Closed
- From: "Gregory A. Lusk" <glusk@mesasub.com>
- Date: Fri, 27 Dec 2002 15:27:20 -0500
- thread-index: AcKtzCEnx/ySrUX1TMizeUqeSJTtBQAB48BAAASiIwA=
- Thread-Topic: How To Tell If a Socket Has Been Closed
One caveat on this solution....after testing, the only network event
that is not detected is unplugging the network cable on the remote host.
I have added code that will detect this condition on the write() method
of an outputstream, but otherwise, I cannot seem to detect it.
Everything else seems to work, though.
Greg Lusk
-----Original Message-----
From: Gregory A. Lusk
Sent: Friday, December 27, 2002 1:22 PM
To: ajug-members@www.ajug.org
Subject: RE: How To Tell If a Socket Has Been Closed
Okay, I have a solution. I haven't thoroughly tested it yet, but it
appears to work upon initial trial.
I have created a class that instantiates a new socket and connects to
the remote host. Upon doing so, if the connection was successful, a new
thread is spawned that continuously reads an inputstream for data. If
the underlying connection is terminated by the remote host, an
IOException is thrown that I can handle. The code is shown below (I
have some other custom classes in here, namely the RFC1006Exception and
RFC1006Socket, but assume they are just generic exception and socket
classes for this example):
==========================
public void connect() throws RFC1006Exception
{
try
{
if ( _isConnected== true)
{
throw new RFC1006Exception("A connection
to the specified host already exists.",5);
}
//initiate an outgoing connection
_Socket = new
RFC1006Socket(_RemoteHostIPAddress,102);
// spawn another thread to continuously read and throw an exception if
the underlying socket is closed
_checker = new CheckerThread(_Socket,this);
_checker.start();
_Socket.setTcpNoDelay(false);
}
catch (java.io.IOException ex)
{
notifyListeners(4);
_isConnected = false;
throw new RFC1006Exception("A connection to the
remote host failed on the RFC1006 connect() method.",4);
}
notifyListeners(3);
_Socket.addRFC1006(this);
_isConnected = true;
}
================
-----Original Message-----
From: Calvin Yu [mailto:cyu77@yahoo.com]
Sent: Friday, December 27, 2002 12:17 PM
To: ajug-members@www.ajug.org
Subject: 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
>
>
>