Multiple streams from one Socket

Discussion in 'Java' started by =?iso-8859-2?B?QWRhtg==?=, Jan 20, 2007.

  1. Hi,

    I was wondering what does exactly methods getInputStream and
    getOutputStream methods in Socket class do?
    I always imagined that there is some fixed stream inside Socket and all
    we get is a reference to it. But then I thought what if I get the
    stream and create instance of some not abstract Stream, close it, and
    then try to open it again.
    eg.

    Socket s = new Socket(...);
    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    Object o = in.readObject();

    [...]

    in.close();

    BufferedReader r = new BufferedReader(new
    InputStreamReader(socket.getInputStream()));
    String str = r.readLine();

    [...]

    r.close();

    would it work?? or would it throw exception because it has already been
    closed?
    The other thing that crossed my mind was what if I'd try to open many
    differed streams from one socket at once? I seriously doubt it would
    work but, of course, I might be wrong :).
     
    =?iso-8859-2?B?QWRhtg==?=, Jan 20, 2007
    #1
    1. Advertising

  2. =?iso-8859-2?B?QWRhtg==?=

    Tom Hawtin Guest

    Ada¶ wrote:
    >
    > I was wondering what does exactly methods getInputStream and
    > getOutputStream methods in Socket class do?
    > I always imagined that there is some fixed stream inside Socket and all
    > we get is a reference to it.


    You could look at the source. PlainSocketImpl.getInputStream gives the
    answer. It's pretty much what you would expect.

    > But then I thought what if I get the
    > stream and create instance of some not abstract Stream, close it, and
    > then try to open it again.


    Closing either stream closes the socket. (Half closing is done through
    Socket.shutdownInput/shutdownOutput.)

    Tom Hawtin
     
    Tom Hawtin, Jan 20, 2007
    #2
    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. Laszlo Nagy
    Replies:
    1
    Views:
    5,114
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,032
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    601
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    716
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    760
Loading...

Share This Page