Socket, InputStream and close

Discussion in 'Java' started by nicolas.edel@gmail.com, Jun 25, 2008.

  1. Guest

    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
    , Jun 25, 2008
    #1
    1. Advertising

  2. Daniel Pitts Guest

    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/>
    Daniel Pitts, Jun 25, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 25, 5:57 pm, Daniel Pitts
    <> wrote:
    > 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
    , Jun 26, 2008
    #3
  4. Guest

    On Jun 26, 9:05 am, "Peter Duniho" <>
    wrote:
    > On Wed, 25 Jun 2008 23:53:52 -0700, <> wrote:
    > > On Jun 25, 5:57 pm, Daniel Pitts
    > > <> wrote:
    > >> 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
    , Jun 26, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. R
    Replies:
    5
    Views:
    2,090
    Kevin McMurtrie
    Mar 13, 2005
  2. Replies:
    8
    Views:
    1,071
    Roedy Green
    Feb 14, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,753
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    940
    Jean-Paul Calderone
    Jan 27, 2009
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    815
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page