Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Multiple streams from one Socket

Reply
Thread Tools

Multiple streams from one Socket

 
 
=?iso-8859-2?B?QWRhtg==?=
Guest
Posts: n/a
 
      01-20-2007
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 .

 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      01-20-2007
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
 
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
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments