Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of Java sockets when closing output stream

Tags:

Can someone explain the following behavior in Java sockets:

The general idea is this:

  1. Open socket, Obtain I/O streams.
  2. Write request, Close out stream
  3. Read Response, Close in stream
  4. Close socket.

Here's my question / issue.

If I use a PrintWriter for output, and then close it, It closes the whole socket, and the subsequent read operation fails miserably.

Instead if I directly use the socket's shutdownOutput() method, it correctly closes the output stream channel, while keeping the socket alive.

Why would closing the PrintWriter object take the whole socket down with it?

like image 906
Lenny Markus Avatar asked Jan 17 '12 05:01

Lenny Markus


People also ask

Does closing stream close Socket?

The Close method frees both unmanaged and managed resources associated with the NetworkStream. If the NetworkStream owns the underlying Socket, it is closed as well. If a NetworkStream was associated with a TcpClient, the Close method will close the TCP connection, but not dispose of the associated TcpClient.

Do we need to close output stream in Java?

Close an OutputStreamOnce you are done writing data to a Java OutputStream you should close it.

Should you close a Socket Java?

The finalize() method is called by the Java virtual machine ( JVM ) before the program exits to give the program a chance to clean up and release resources. Multi-threaded programs should close all Files and Sockets they use before exiting so they do not face resource starvation.

Which method closes Socket?

close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.


2 Answers

This may be what your code looks like:

Socket socket; PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.close(); 

Now, let's have a look at the description of getOutputStream() method of Socket.

getOutputStream

public OutputStream getOutputStream() throws IOException

Returns an output stream for this socket. If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an IllegalBlockingModeException.

Closing the returned OutputStream will close the associated socket.

Returns:

an output stream for writing bytes to this socket.

Throws:

IOException - if an I/O error occurs when creating the output stream or if the socket is not connected.

from the description above, we know closing the returned OutputStream will close the associated socket.

Now, when you close the PrintWriter, it'll close the associated OutputStream which will close the associated socket.

like image 161
Alanmars Avatar answered Nov 16 '22 12:11

Alanmars


It is probably because calling the close() method of PrintWriter is tracing back through the hierarchy and calling the close() method of the SocketOutputStream as well. As part of the close() method for the SocketOutputStream it also calls the close() method for the Socket as well, which would in term close the SocketInputStream as well. Calling the shutdownOutput() function instead sends any previously written data followed by TCP's normal connection termination sequence. It then disables the output stream.

like image 32
aoi222 Avatar answered Nov 16 '22 12:11

aoi222