Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > socket and bufferedreader close()

Reply
Thread Tools

socket and bufferedreader close()

 
 
-
Guest
Posts: n/a
 
      04-06-2005
i have a

socket = ...
br = new BufferedReader(...


when i close, should i close both br and socket? if so, in which order?
if not, which one should be closed?
 
Reply With Quote
 
 
 
 
Arnaud Berger
Guest
Posts: n/a
 
      04-06-2005
Hi,

You should obviously close the reader first, because if the stream it is
reading from is closed ,it will probably get you IOException .

You should also close the socket.

Regards,

Arnaud


"-" <(E-Mail Removed)> a écrit dans le message news:
425347d9$(E-Mail Removed)...
> i have a
>
> socket = ...
> br = new BufferedReader(...
>
>
> when i close, should i close both br and socket? if so, in which order?
> if not, which one should be closed?



 
Reply With Quote
 
 
 
 
Steve Horsley
Guest
Posts: n/a
 
      04-06-2005
- wrote:
> i have a
>
> socket = ...
> br = new BufferedReader(...
>
>
> when i close, should i close both br and socket? if so, in which order?
> if not, which one should be closed?


Closing the BufferedReader should also close the underlying Reader, which
should close the underlying InputStream which should close the underlying Socket.

But if you also called the socket's getOutputStream() method, you should also
close that stream (or any wrappers you put round it) to avoid leaving an open
InputStream / file handle. I think that closing the socket doesn't close the
input and output streams above it.

Steve
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      04-06-2005
Steve Horsley wrote:
>>
>> when i close, should i close both br and socket? if so, in which
>> order? if not, which one should be closed?

>
> Closing the BufferedReader should also close the underlying Reader, which
> should close the underlying InputStream which should close the
> underlying Socket.
>
> But if you also called the socket's getOutputStream() method, you should
> also
> close that stream (or any wrappers you put round it) to avoid leaving an
> open
> InputStream / file handle. I think that closing the socket doesn't close
> the input and output streams above it.


Always close the output stream first, as this will flush any pending
data. You can then close the input stream and the socket if you want to
but there is no need, you won't be able to use them anyway.
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      04-07-2005
Esmond Pitt wrote:

> Always close the output stream first, as this will flush any pending
> data.


Thanks for the tip.

> You can then close the input stream and the socket if you want to
> but there is no need, you won't be able to use them anyway.


Are you sure this doesn't constitute a handle leak if you don't
close the input stream too? I was sure I got "too many open files"
errors once after opening and closing lots of sockets but forgetting
to close the associated streams.

Steve
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      04-08-2005
Steve Horsley wrote:
> Are you sure this doesn't constitute a handle leak if you don't
> close the input stream too? I was sure I got "too many open files"
> errors once after opening and closing lots of sockets but forgetting
> to close the associated streams.


No, closing either of the streams also closes the socket.
 
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