Socket, InputStream and close

N

nicolas.edel

I am wondering why the hell the SocketInputStream.close()
implementation uses socket.close () instead of
socket.shutdownInput() . I just want to close the input, not the
output either !
(I'm using sun java 1.5.0_15-b04)
Any idea ?

-Nicolas
 
D

Daniel Pitts

I am wondering why the hell the SocketInputStream.close()
implementation uses socket.close () instead of
socket.shutdownInput() . I just want to close the input, not the
output either !
(I'm using sun java 1.5.0_15-b04)
Any idea ?

-Nicolas
Don't close the SocketInputStream then, shutdownInput() manually, or
leave it open.
 
N

nicolas.edel

Don't close the SocketInputStream then, shutdownInput() manually, or
leave it open.

I can't : this InputStream is used by an XML SAX parser (from
org.xml.sax package) that calls the close method at the end of
parsing. I just wrote a wrapper to this InputStream that don't close
the socket and that's ok. But I was just wondering if closing the
socket when just asking to close the *input* side should not been
considered as a painful (conceptual?) bug ...

-Nicolas
 
N

nicolas.edel

I can't : this InputStream is used by an XML SAX parser (from
org.xml.sax package) that calls the close method at the end of
parsing. [...] But I was just wondering if closing the
socket when just asking to close the *input* side should not been
considered as a painful (conceptual?) bug ...

I'm a bit puzzled by your question.  As near as I can tell,  
SocketInputStream is part of the Apache library, and it exists for the  
sole purpose of providing a stream that will automatically close the  
underlying socket when the stream itself is closed.

The docs I'm looking at indicate that you have to provide both a Socket  
reference as well as an existing InputStream reference when you  
instantiate the SocketInputStream.  So presumably you already have a valid  
InputStream you _could_ use for parsing.

Why is it that you are using SocketInputStream, given that the one thing  
the class appears to do is the one thing that bothers you about it?  Why  
can't you just pass the original InputStream to the parser?

Pete

I don't use SocketInputStream explicitely but socket.getInputStream().
The Eclispe debugger then told me a SocketInputStream was used inside
the SAX parser.
But just consider the simple program:
public class foo
{
public static void main (String args[]) {
if (args.length!=2) {
System.err.println ("Usage: java foo <host> <port>");
System.exit (1);
}
try {
java.net.Socket client;
client = new java.net.Socket (sargs[0], new
Integer(args[1]));
client.getInputStream().close ();
System.in.read ();
client.getOutputStream().write ("foo".getBytes());
} catch (Exception e) {
e.printStackTrace ();
}
}
}

As soon as I call getInputStream().close(), I can see (using tcpdump)
a FIN TCP packet being sent, what clearly means the socket output has
been closed.
Whatever server I use (ie even a server that never close its
connections), this simple program will always output:
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:803)
at foo.main(foo.java:13)

-Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top