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 setReuseAddress () method The setReuseAddress () method of Java Socket class enables or disables the SO_REUSEADDR socket option. The initial setting of SO_REUSEADDR is disabled. Syntax public void setReuseAddress (boolean on) throws SocketException Parameter The parameter 'on' states whether the SO_REUSEADDR option should be enabled or disabled. Return NA Throws SocketException -...
3 min read
Java method The isInputShutdown () method of Java Socket class returns a Boolean value 'true' if the read-half of the socket connection has been closed, else it returns false. Syntax public boolean isInputShutdown () Parameter NA Return The isInputShutdown () method returns a Boolean value 'true' if the socket has been...
3 min read
Java The Socket class acts as an endpoint for communication between two machines. It implements client sockets. Methods Method Description bind(SocketAddress bindpoint) This method binds the given socket to the specified local address. close() This method closes the socket. connect(SocketAddress endpoint) connect(SocketAddress endpoint, int timeout) The connect() method connects this socket to the server. This method...
8 min read
Java method The shutdownOutput() method of Java Socket class disables the output stream for the specified socket. If the socket output stream is written after invoking the shutdownOutput() method on the socket, the stream will throw an IOException. Syntax public void shutdownOutput () throws IOException Parameter NA Return NA Throws IOException...
2 min read
Java method The getTcpNoDelay () method of Java Socket class returns the setting for SO_TIMEOUT option if it is enabled, else it returns false. Syntax public boolean getTcpNoDelay () Parameter NA Return The getTcpNoDelay () method returns a Boolean value 'true' if the TCP_NODELAY is enabled. Throws SocketException - if an error is...
2 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
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 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 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
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