Java’s Socket class gives you control over how a socket handles queued data when it closes. This linger-on-close option allows you to dictate whether you want the socket to send out queued data to its peer when the close() method is invoked, and if so, how long after the invocation of the close() call should the socket try to send the data before actually closing the socket. The linger-on-close option is controlled using two methods defined in the Socket class:
public int getSoLinger() throws SocketExceptionpublic void setSoLinger(boolean lingerOnClose, int timeout) throws SocketException
The first method returns the timeout that is set for lingering on a socket when it closes. The second one sets the lingerOnClose option and the timeout for the linger-on-close. If the lingerOnClose argument is true, the close() call blocks until any data that has been queued for transmission has been sent, or until the timeout has expired. If the timeout expires, any unsent data is discarded. If the lingerOnClose argument is false, the close() call returns immediately and all queued data is discarded.