Java Socket setSoLinger() method25 Mar 2025 | 2 min read The 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 1Output: Timeout value: 90 Example 2Output: Timeout value: -1 Example 3Output: Exception in thread "main" java.lang.IllegalArgumentException: invalid value for SO_LINGER at java.net.Socket.setSoLinger(Socket.java:1021) at com.tpointtech.JavaSocketSetSoLingerExample3.main(JavaSocketSetSoLingerExample3.java:14) Example 4Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.setSoLinger(Socket.java:1016) at com.tpointtech.JavaSocketSetSoLingerExample4.main(JavaSocketSetSoLingerExample4.java:18) Next TopicJava Socket Class |
Java method The getLocalSocketAddress() method of Java Socket class returns the address of the endpoint to which this socket is bound. This method will continue to return an InetSocketAddress even after the socket is closed. Syntax public SocketAddress getLocalSocketAddress () Parameter NA Return The getLocalSocketAddress() method returns: the SocketAddress, representing the...
3 min read
Java method The getChannel() method of Java Socket class returns the unique SocketChannel object linked with this socket. Syntax public SocketChannel getChannel() Parameter NA Return The getChannel() method either returns the socket channel linked with this socket or returns null if the socket was not created for the specified channel. Example...
2 min read
Java method The getSendBufferSize () method of Java Socket class returns the buffer size(SO_SNDBUF) used by the platform for output on this socket. Syntax public int getSendBufferSize () Parameter NA Return The getSendBufferSize () method returns the value of the SO_SNDBUF option of the specified Socket. Throws SocketException - if an error...
2 min read
Java method The getKeepAlive() method of Java Socket class tests whether SO_KEEPALIVE option is enabled or not. Syntax public boolean getKeepAlive () Parameter NA Return The getKeepAlive() method either returns a Boolean indicating whether SO_KEEPALIVE option is enabled or not. Throws SocketException - if there is an error in the underlying protocol, such...
2 min read
Java method connect(SocketAddress endpoint) The connect() method of Java Socket class connects the specified socket to the server. Syntax public void connect(SocketAddress endpoint) throws IOException Parameter The parameter 'endpoint' represents the SocketAddress. Return NA Throws IOException - if an error occurs during the connection. IllegalBlockingModeException - if this socket has an associated channel and the...
5 min read
Java method The setOOBInline() method of Java Socket class enables or disables the SO_OOBInline. By default, the SO_OOBInline option is disabled. Syntax public void setOOBInline (Boolean on) throws SocketException Parameter The 'on' represents whether the SO_OOBInline option should enabled or not. Return NA Throws SocketException - if there is an error in the...
2 min read
Java method The getInputStream() method of Java Socket class returns an input stream for the given socket. If you close the returned InputStream, then it will close the linked socket. Syntax public InputStream getInputStream () throws IOException Parameter NA Return The getInputStream() returns an input stream for reading bytes...
3 min read
Java method The setSoTimeout () method of Java Socket class enables or disables the SO_TIMEOUT option with the given timeout value, in milliseconds. The timeout value should be greater than 0 otherwise, it will throw an error. Syntax public void setSoTimeout (int timeout) throws SocketException Parameter The...
3 min read
Java method The setReceiveBufferSize() method of Java Socket class sets the SO_RCVBUF option to the given value for the specified socket. The buffer size should be greater than zero else, it will throw an IllegalArgumentException. Syntax public void setReceiveBufferSize (int size) throws SocketException Parameter The parameter 'size' represents the...
2 min read
Java method The isConnected () method of Java Socket class returns the connection state of the socket. This method would continue to return the connection state which was there before being closed. Syntax public boolean isConnected() Parameter NA Return The isConnected () method would return a Boolean value 'true' if the...
3 min read
We request you to subscribe our newsletter for upcoming updates.
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