Enable/disable so_linger with the specified stay time (in seconds. The maximum timeout value is platform-specific. This setting only affects socket disabling. The default value is-1, which indicates disabled.
This socket option can affect the behavior of the close method. By default, when the close method is called, the data packets will be returned immediately. If there are still unsent data packets, the data packets will be discarded. If you set the linger parameter to a positive integer N (the maximum value of N is 65,535), the method will be blocked for up to n seconds after you call the close method. In these n seconds, the system will try to Send unsent packets out; if more than n seconds are passed, if there are still unsent packets, all these packets will be discarded; the close method returns immediately. If you set the linger to 0, the function is the same as if you disable the so_linger option.
Package socket; import Java. io. *; import java.net. socket; import java.net. unknownhostexception; import Org. apache. log4j. logger; public class test_solinger {Private Static logger = logger. getlogger (test_solinger.class); public static void main (string [] ARGs) {bufferedreader in = NULL; printwriter out = NULL; try {string IP = "127.0.0.1 "; socket socket = new socket (IP, 10000); In = new bufferedreader (ne W inputstreamreader (socket. getinputstream (); out = new printwriter (socket. getoutputstream (); logger. debug (socket. getsolinger ();/*** the socket is called here. when close () is returned, hello and world may not have been successfully sent to the other party. If we set linger instead of less than 0, close will not be returned until the sent data has been confirmed. * If the other party goes down and times out, the response is returned based on the time set by the linger. * The server will also receive the message */Boolean on = true; int linger = 100; socket. setsolinger (on, linger); string head = "hello"; string body = "world \ r \ n"; out. write (head); out. write (body); socket. close ();} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}}