Java Socket setSoLinger() methodThe setSoLinger() method of Java Socket class enables or disables the SO_LINGER option with the given linger time in seconds. It is used to specify how the close() method affects socket using a connection-oriented protocol. The timeout value is platform-specific, and this setting only affects the socket close. SyntaxParameteron - whether the 'linger' should be enabled or not.M linger - how long to linger for, if the parameter 'on' is true. ReturnNA ThrowsSocketException - if an error is there in the underlying protocol, such as a TCP error. IllegalArgumentException - if the linger value is less than 0. Example 1Test it NowOutput: Timeout value: 90 Example 2Test it NowOutput: Timeout value: -1 Example 3Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: invalid value for SO_LINGER at java.net.Socket.setSoLinger(Socket.java:1021) at com.javaTpoint.JavaSocketSetSoLingerExample3.main(JavaSocketSetSoLingerExample3.java:14) Example 4Test it NowOutput: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.setSoLinger(Socket.java:1016) at com.javaTpoint.JavaSocketSetSoLingerExample4.main(JavaSocketSetSoLingerExample4.java:18) Next TopicJava Socket Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India