Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Socket, InputStream and close

Reply
Thread Tools

Socket, InputStream and close

 
 
nicolas.edel@gmail.com
Guest
Posts: n/a
 
      06-25-2008
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
nicolas.edel@gmail.com
Guest
Posts: n/a
 
      06-26-2008
On Jun 25, 5:57*pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


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
 
Reply With Quote
 
nicolas.edel@gmail.com
Guest
Posts: n/a
 
      06-26-2008
On Jun 26, 9:05*am, "Peter Duniho" <(E-Mail Removed)>
wrote:
> On Wed, 25 Jun 2008 23:53:52 -0700, <(E-Mail Removed)> wrote:
> > On Jun 25, 5:57*pm, Daniel Pitts
> > <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >> > 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 !

>
> >> 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. [...] 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
Browser close event (MSIE and FF): how to detect browser close ? pamelafluente@libero.it Javascript 0 01-23-2007 12:44 AM
confused: Socket InputStream != ServerSocker InputStream R Java 5 03-13-2005 07:26 AM
JavaMail POP3 folder.close() method close also store! Daniel Albisser Java 1 04-07-2004 03:45 PM
How to close child browser while parent close? Denon ASP .Net 1 11-14-2003 08:14 AM



Advertisments