Search code examples
javasocketsstreaminputstreamoutputstream

Does closing the socket close the stream?


  1. I am working in a legacy java application, In many files, socket and streams are used, where sockets are getting closed but not the streams, is this necessary to close all the streams before closing the socket. because I am getting "too many open files error", is this error because of not closing the streams.....

  2. closing the socket will automatically close the streams also?


Solution

  • From the Socket Javadoc:

    Closing this socket will also close the socket's InputStream and OutputStream.

    So generally speaking, closing the socket should be enough to close both streams it created.

    Your "too many open files error" is most likely caused by something else.