Java Socket setKeepAlive() method25 Mar 2025 | 2 min read The setKeepAlive() method of Java Socket class returns a Boolean value 'true' if the write-half of the socket connection has been closed successfully else it returns false. SyntaxParameterThe parameter 'on' represents whether the socket should be enabled or not. ReturnNA ThrowsSocketException - if an error is there in the underlying protocol, such as a TCP error. Example 1Output: SO_KEEPALIVE is enabled: true Example 2Output: SO_KEEPALIVE is not enabled... Example 3Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.setKeepAlive(Socket.java:1309) at com.tpointtech.JavaSocketSetKeepAliveExample3.main(JavaSocketSetKeepAliveExample3.java:13) Next TopicJava Socket Class |
Java Socket setSendBufferSize () method The setSendBufferSize () method of Java Socket class sets the SO_SNDBUF option to the given value for this socket. The size value should be greater than 0. Syntax public void setSendBufferSize (int size) throws SocketException Parameter The parameter 'size' represents the size to which the...
2 min read
Java method The getRemoteSocketAddress () method of Java Socket class returns the address of the endpoint of the specified socket if it is connected else it will return null if this socket is not connected. Syntax public SocketAddress getRemoteSocketAddress () Parameter NA Return The getRemoteSocketAddress () method either returns the SocketAddress...
3 min read
Java method The getOutputStream() method of Java Socket class returns an output stream for the given socket. If you close the returned OutputStream then it will close the linked socket. Syntax public OutputStream getOutputStream() throws IOException Parameter NA Return The getOutputStream() returns an output stream for writing bytes to this socket. Throws IOException-...
3 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 toString() method of Java Socket class converts this socket to a String. Syntax public void toString() Parameter NA Overrides The toString() method in class Object Return The toString() method returns the string representation for the given socket. Example 1 import java.io.IOException; import java.net.*; public class JavaSocketToStringExample1 { ...
3 min read
Java method The getPort() method of Java Socket class returns the remote port number to which the socket is connected. This method will return the port number even after the socket is closed. Syntax public int getPort () Parameter NA Return The getPort() method either returns the port number to which...
2 min read
Java method The setTrafficClass() method of Java Socket class sets the traffic class or type-of-service in the IP header for packets sent from the specified socket. The parameter (tc) should be in the range from 0 to 255(0<=tc<=255) else an IllegalArgumentException will be thrown. Syntax public void...
3 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 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...
4 min read
Java method The getLocalPort() method of Java Socket class returns the local port number to which the specified socket is bound. If the socket was being closed, this method will return the connected port number after the socket is closed. Syntax public int getLocalPort() Parameter NA Return The getLocalPort () method...
2 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