Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Closing sockets and streams?

Reply
Thread Tools

Closing sockets and streams?

 
 
Knute Johnson
Guest
Posts: n/a
 
      01-21-2006
If I have a socket and an InputStream and OutputStream that I have
opened from that socket, do I need to close the streams when I am
finished if I close the socket?

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      01-21-2006
out.close();
in.close();
clientSocket.close();
serverSocket.close();

from
http://java.sun.com/docs/books/tutor...entServer.html

protected void finalize(){
//Clean up
try{
in.close();
out.close();
server.close();
} catch (IOException e) {
System.out.println("Could not close.");
System.exit(-1);
}
}

from
http://java.sun.com/developer/online...ketServer.java


It appears it is recommended.



Opalinski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.geocities.com/opalpaweb/

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-21-2006
(E-Mail Removed) opalinski from opalpaweb wrote:
> out.close();
> in.close();
> clientSocket.close();
> serverSocket.close();
>
> from
> http://java.sun.com/docs/books/tutor...entServer.html
>
> protected void finalize(){
> //Clean up
> try{
> in.close();
> out.close();
> server.close();
> } catch (IOException e) {
> System.out.println("Could not close.");
> System.exit(-1);
> }
> }
>
> from
> http://java.sun.com/developer/online...ketServer.java
>
>
> It appears it is recommended.
>
>
>
> Opalinski
> (E-Mail Removed)
> http://www.geocities.com/opalpaweb/
>


Excellent! Thank you.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-21-2006
On Fri, 20 Jan 2006 16:22:19 -0800, Knute Johnson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>If I have a socket and an InputStream and OutputStream that I have
>opened from that socket, do I need to close the streams when I am
>finished if I close the socket?


You close the last thing you opened, and it deals with closing all the
things you built up to it.

So, for example, closing a buffered output stream closes the embedded
output stream.

close calls super.close() all the way down like turtles.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-21-2006
Roedy Green wrote:
>
> You close the last thing you opened, and it deals with closing all the
> things you built up to it.
>
> So, for example, closing a buffered output stream closes the embedded
> output stream.
>
> close calls super.close() all the way down like turtles.


If the construction of the buffered output stream fails, then you have
to remember to close the underlying stream. However, you can wrap this
up in a convenience method.

A simpler idiom is: for output streams, flush the most wrapped output
stream in the happy case. close the underlying stream within a finally
block.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      01-22-2006
Thomas Hawtin wrote:
> Roedy Green wrote:
>
>>
>> You close the last thing you opened, and it deals with closing all the
>> things you built up to it.
>>
>> So, for example, closing a buffered output stream closes the embedded
>> output stream.
>>
>> close calls super.close() all the way down like turtles.

>
>
> If the construction of the buffered output stream fails, then you have
> to remember to close the underlying stream. However, you can wrap this
> up in a convenience method.
>
> A simpler idiom is: for output streams, flush the most wrapped output
> stream in the happy case. close the underlying stream within a finally
> block.
>
> Tom Hawtin


If you don't close them will they eventually get closed in the garbage
collector or are they left dangling forever?

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      01-22-2006

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:JGBAf.12678$(E-Mail Removed)...

> If you don't close them will they eventually get closed in the garbage
> collector or are they left dangling forever?


Since the GC may never run, worst case is that they stay open. So it's wise
to close them youeself when you're done with them.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-22-2006
On Sat, 21 Jan 2006 18:00:07 -0800, Knute Johnson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>
>If you don't close them will they eventually get closed in the garbage
>collector or are they left dangling forever?


I don't know. I have always presumed they could dangle unclosed
forever.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      01-22-2006
"(E-Mail Removed) opalinski from opalpaweb" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) ups.com:

>
> protected void finalize(){
> //Clean up
> try{
> in.close();
> out.close();
> server.close();
> } catch (IOException e) {
> System.out.println("Could not close.");
> System.exit(-1);
> }
> }
>


No! Don't ever use finalizers to close critical resources. In fact, don't
use finalizers at all - there are very few legitimate reasons to ever use
one. Finalizers are not guaranteed to be called at any time, in any order,
or at all!

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-23-2006
Knute Johnson wrote:
> Thomas Hawtin wrote:
>> Roedy Green wrote:
>>> So, for example, closing a buffered output stream closes the embedded
>>> output stream.


>> A simpler idiom is: for output streams, flush the most wrapped output
>> stream in the happy case. close the underlying stream within a finally
>> block.


> If you don't close them will they eventually get closed in the garbage
> collector or are they left dangling forever?


If a tree falls in a wood with nobody about to here it, does it make any
noise?

It's not a question of when they get closed by the garbage collector.
They will have no chance at all of being closed by the garbage
collector. (Or more strictly, in a finalizer thread.) They have no
finaliser (nor other forms of reference handlers). They are not
resource, so it does not matter. Insisting on closing them is just voodoo.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
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
Problem closing sockets Will C Programming 2 05-03-2006 05:37 AM
closing sockets Mohammed Smadi Python 2 10-04-2005 06:25 PM
Closing programs that use sockets billiejoex Python 2 08-27-2005 09:51 AM
telnetlib close not closing sockets Mike Monaghan Python 4 10-07-2004 01:01 AM
urllib2 - closing sockets Anand Pillai Python 3 06-05-2004 05:17 PM



Advertisments