Multiple streams from one Socket

  • Thread starter =?iso-8859-2?B?QWRhtg==?=
  • Start date
?

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

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 :).
 
T

Tom Hawtin

Ada¶ said:
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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top