Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Feb 24, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Lingering on Sockets

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 SocketException
public 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.
Ajit Sagar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date