Article View: borland.public.jbuilder.java.api
Article #1293Re: Dead Socket doesn't throw exception
From: Joi Ellis [TeamB]
Date: Mon, 01 Apr 2002 14:32
Date: Mon, 01 Apr 2002 14:32
90 lines
3715 bytes
3715 bytes
Owen Green <owen@begonefoulmachine.kennulz.demon.co.uk> wrote:
> Hi,
> I'm having a bit of trouble reliably detecting when a socket has been
> disconnected from the other end. I've seen various opinions around the place
> that a read or write should result in an exception when this has happened,
> but the code below fails to produce one (at least on my system which is,
> gack, w95).
As I understand it, when a remote socket shuts down normally, your IO streams
will see EOF markers, not exceptions. You'll get an exception if the
socket dies without being properly closed, IE the remote program crashed
and didn't clean up, the host crashed, or some device tossing packets between
the two hosts has stopped cooperating. That's when you get a socket
exception.
Don't you have to read once before you can get an EOF indication?
What happens if you alter the test as follows? (Look for //### comments.)
> I'm a bit stumped on what to do with this as there seems to be /no/ reliable
> method for detecting this condition.
> import java.net.*;
> import java.io.IOException;
> import java.util.Date;
> public class SocketTest implements Runnable{
> public void run(){
> ServerSocket ss = null;
> try {
> ss = new ServerSocket(6666);
> Socket s = ss.accept();
s.getOutputStream.println("this is a test"); //###
> //disconnect immediately
> s.shutdownInput();
> s.shutdownOutput();
> s.close();
> s = null;
> System.err.println(new Date().toString() + ": Shut Socket");
> }
> catch (IOException ex) {
> ex.printStackTrace();
> }
> finally {
> if(ss!=null){
> try{
> ss.close();
> System.err.println(new Date().toString() + ": Shut Server");
> }
> catch(IOException ex){
> ex.printStackTrace();
> }
> }
> }
> }
> public static void main(String[] args) {
> new Thread(new SocketTest()).start();
> try{
> Socket s = new Socket("127.0.0.1",6666);
> try{
> Thread.sleep(5000);
> }catch(InterruptedException ie){
> ie.printStackTrace();
> }
> //will return -1 (indicating that there's nothing to read),
> //BUT won't throw exception even though socket is defunct
for (;;) { //###
> System.err.println(s.getInputStream().read());
} //###
> }catch(IOException ex){
> ex.printStackTrace();
> }
> }
> }
--
Joi Ellis (TeamB) http://www.teamb.com/
No direct email, please.
For best results, post on news server newsgroups.borland.com!
Newsgroup Guidelines: http://www.borland.com/newsgroups/guide.html
Only posts which violate the rules in guide.html are subject to cancelation.
Technical Details
Message-ID:
<3ca86f83_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!fu-berlin.de!news.boerde.de!nntp.cs.uni-magdeburg.de!PeerFeed.KWeb.on.ca!dnews!not-for-mailReferences:
<3ca7b409_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,a31ecd0db1134a2e
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-04-01 06:34:35 PST
Sender: Joi Ellis <joi@artoo.gyles19.visi.com>
Message-ID: <3ca86f83_1@dnews>
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: Dead Socket doesn't throw exception
Newsgroups: borland.public.jbuilder.java.api
References: <3ca7b409_1@dnews>
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.2.19-6.2.7 (i686))
NNTP-Posting-Host: 209.98.6.107
Date: 1 Apr 2002 06:32:36 -0800
X-Trace: dnews 1017671556 209.98.6.107 (1 Apr 2002 06:32:36 -0800)
Lines: 90
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!fu-berlin.de!news.boerde.de!nntp.cs.uni-magdeburg.de!PeerFeed.KWeb.on.ca!dnews!not-for-mail