V
Vuun Harjnes
Hi,
I'm having trouble detecting when a socket gets closed. I've created a
ServerSocket and a Socket but when either side closes their socket I don't
know how to detect this at the other side. I would have thought that the
isConnected() or isClosed() methods would tell me but their state doesn't
change at all once the other side of the connection closes the connection
down.
At first I thought this might be a bug but I've tried it on j2sdk 1.4.2_01
and 1.4.2_03. (On a Debian 3.0r1 box)
Doing it across two machines I can see (via ethereal) that the socket gets
created and torn down correctly.
I've got a bad feeling that its something simple that I've overlooked but I
just can't see what it is. I'd appreciate any help anyone can give me.
Regards
Haan.
Here's my sample code
serv.java
Creates a server socket at port 7777, listens for a single connection and
reports the state of that connection every 300ms.
----------------
import java.io.*;
import java.net.*;
public class serv
{
public static void main(String[] args)
{
try
{
ServerSocket sS = new ServerSocket(7777);
Socket s = sS.accept();
while (true)
{
System.out.println("" + s.isOutputShutdown() + " " +
s.isInputShutdown() + " " +
s.isClosed() + " " +
s.isConnected() + " " +
s.isBound());
try
{
Thread.sleep(300);
} catch (Exception e)
{
System.out.println(e);
}
}
} catch (Exception e)
{
System.out.println(e);
}
}
}
sock.java
Connects to the server socket created above and closes the connection after
4 seconds.
--------------------
import java.io.*;
import java.net.*;
public class sock
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1", 7777);
try
{
Thread.sleep(4000);
} catch (Exception e)
{
}
s.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
I'm having trouble detecting when a socket gets closed. I've created a
ServerSocket and a Socket but when either side closes their socket I don't
know how to detect this at the other side. I would have thought that the
isConnected() or isClosed() methods would tell me but their state doesn't
change at all once the other side of the connection closes the connection
down.
At first I thought this might be a bug but I've tried it on j2sdk 1.4.2_01
and 1.4.2_03. (On a Debian 3.0r1 box)
Doing it across two machines I can see (via ethereal) that the socket gets
created and torn down correctly.
I've got a bad feeling that its something simple that I've overlooked but I
just can't see what it is. I'd appreciate any help anyone can give me.
Regards
Haan.
Here's my sample code
serv.java
Creates a server socket at port 7777, listens for a single connection and
reports the state of that connection every 300ms.
----------------
import java.io.*;
import java.net.*;
public class serv
{
public static void main(String[] args)
{
try
{
ServerSocket sS = new ServerSocket(7777);
Socket s = sS.accept();
while (true)
{
System.out.println("" + s.isOutputShutdown() + " " +
s.isInputShutdown() + " " +
s.isClosed() + " " +
s.isConnected() + " " +
s.isBound());
try
{
Thread.sleep(300);
} catch (Exception e)
{
System.out.println(e);
}
}
} catch (Exception e)
{
System.out.println(e);
}
}
}
sock.java
Connects to the server socket created above and closes the connection after
4 seconds.
--------------------
import java.io.*;
import java.net.*;
public class sock
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1", 7777);
try
{
Thread.sleep(4000);
} catch (Exception e)
{
}
s.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}