Behavior of Java sockets when closing output stream

前端 未结 2 376
离开以前
离开以前 2020-12-09 16:42

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 ou
2条回答
  •  情歌与酒
    2020-12-09 17:08

    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.

提交回复
热议问题