Socket's close() doesn't close connection (old bug?)
Martin Egholm Nielsen
martin@egholm-nielsen.dk
Tue Nov 8 15:02:00 GMT 2005
Hi,
In my eager to reproduce another bug I've stumbled across yet another
strange issue - namely that Socket's close() doesn't close underlying
socket (with no traffic), nor "wake up" a blocked read-thread (as a
consequence)...
That is, I create server-socket on my server (!), wait for a client to
connect. When the client is connected, I start a new thread waiting for
two seconds before trying to close the socket. Meanwhile, I make the
main-thread get caught in the socket's inputstream's read().
For a client I just use telnet to the given port 123, and do nothing.
On Sun's VM (on i386) it does as expected, but with GCJ (on PPC) the
read() is stuck until I try sendind data - e.g. pressing enter...
After calling close(), "netstat" still reports the connection as
"Established".
However, this may be an old bug, since I'm still stuck with an old
3.4.3. But but but, I've tried fetching natPlainSocketImplPosix.cc and
PlainSocketImpl.java from CVS and that's the same result.
Maybe it's even related to my kernel?!
I'm suspecting it may be related to the discussion in:
http://gcc.gnu.org/ml/java/2004-09/msg00152.html
Moreover, in the same thread, David Daney claims that:
Reads are usless for detecting connections that have not terminated
"normally". Doing a write is the only reliable method for determining
that a connection is still "open".
However, I reckon this is a matter of "normal" termination, right!?
BR,
Martin Egholm
============ Server4.java ============
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server4 extends Thread
{
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(123);
final Socket s = ss.accept();
Thread closerThread = new Thread()
{
public void run()
{
System.out.println("Sleeping before closing...");
try
{
Thread.sleep(2000);
} // try
catch (InterruptedException e)
{
e.printStackTrace();
} // catch
System.out.println("Done sleeping! Closing... " + s.getClass());
try
{
s.close();
} // try
catch (IOException e)
{
e.printStackTrace();
} // catch
System.out.println("Done closing!");
} // run
}; // closerThread
closerThread.start();
try
{
InputStream is = s.getInputStream();
System.out.println("Reading...");
is.read();
System.out.println("Done reading!");
} // try
catch (IOException e)
{
System.out.println("An exception!");
e.printStackTrace();
} // catch
System.out.println("And out of reader!");
} // main
} // Server4
More information about the Java
mailing list