Java Socket parameters: solinger

Source: Internet
Author: User

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 ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.