Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a Socket with Streams

The following question I have is pretty straightforward.

Here is my code:

public class Protocol implements Runnable {

    private SSLSocket socket = null;
    private InputStream is = null;
    private OutputStream os = null;

    ...

    public Protocol(Socket s) {
        socket = (SSLSocket)s;
        is = socket.getInputStream();
        os = socket.getOutputStream();
    }


    @Override
    public void run() {

        ...
        ping();
        socket.close();
        ...
    }


    public void ping() {

        BufferedWriter writer;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write("OK");
        } 
        catch (IOException e) { System.out.println("ERROR: " + e.getLocalizedMessage()); }
        finally { writer = null; }
    }

I understand I didn't include a lot of source code, but this should be enough to answer the question. As you can see, in the "ping" method I have a BufferedWriter which I create to write an "OK" string to the remote source. Later on, I close the Socket.

So my simple question is this - From what I understand, since I close the socket, the chain should go like this:

Close socket ----> which closes is and os ----> closes writer

So, by closing the Socket, I am also closing and allowing the BufferedWriter to be freed by the GC. Am I understanding this correctly or doing something wrong? Is this true for all writers and readers that I initialize in other methods (i.e. BufferedInputStream). And by setting these variables null at the end of the method, am I helping the GC to distinguish between what should be freed? Or should I not do this?

Thanks!

like image 781
Zack Avatar asked Apr 18 '26 06:04

Zack


1 Answers

From what I understand, since I close the socket, the chain should go like this:

Close socket ----> which closes is and os ----> closes writer

No. The BufferedWriter is wrapped around the socket output stream, but the socket doesn't know that. It has no way of closing it.

So, by closing the Socket, I am also closing and allowing the BufferedWriter to be freed by the GC.

No and no. The BufferedWriter is available for GC as soon as ping() returns, and it is never closed at all.

Am I understanding this correctly

No.

or doing something wrong?

Yes. You shouldn't create a new BufferedWriter per message. You should use the same one for the life of the socket, and close it instead of the socket. Similarly you should only use one input stream or Reader for the life of the socket. Otherwise you can lose data in their buffers.

Is this true for all writers and readers that I initialize in other methods (i.e. BufferedInputStream).

No.

And by setting these variables null at the end of the method, am I helping the GC to distinguish between what should be freed?

No. You are just wasting time and space. The method is about to exit anyway, so all its local variables disappear.

Or should I not do this?

You should not do any of it.

like image 162
user207421 Avatar answered Apr 19 '26 20:04

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!