Java Socket setSoTimeout() method25 Mar 2025 | 2 min read 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. SyntaxParameterThe parameter 'timeout' represents a specific timeout in milliseconds. ReturnNA ThrowsSocketException - if an error is there in the underlying protocol, such as a TCP error. Example 1Output: Timeout value: 90987789 Example 2Output: Timeout value: 0 Example 3Output: Exception in thread "main" java.lang.IllegalArgumentException: timeout can't be negative at java.net.Socket.setSoTimeout(Socket.java:1139) at com.tpointtech.JavaSocketSetSoTimeoutExample3.main(JavaSocketSetSoTimeoutExample3.java:13) Example 4Test it NowOutput: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.setSoTimeout(Socket.java:1137) at com.tpointtech.JavaSocketSetSoTimeoutExample4.main(JavaSocketSetSoTimeoutExample3.java:16) Next TopicJava Socket Class |
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 getSoLinger () method of Java Socket class returns the setting if the SO_Linger option is set else it returns -1 if the option is disabled. This method only affects the setting for SO_LINGER. Syntax public int getSoLinger () Parameter NA Return The getSoLinger () method returns the setting for...
3 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 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 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 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 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 setTcpNoDelay () method of Java Socket class enables or disables the TCP_NODELAY option. Syntax public void setTcpNoDelay (boolean on) throws SocketException Parameter The parameter 'on' represents enables or disables the TCP_NODELAY option. Return NA Throws SocketException - if an error is there in the underlying protocol, such as...
3 min read
Java method 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. Syntax public void setKeepAlive (boolean on) throws SocketException Parameter The parameter 'on' represents whether the socket should be enabled or...
2 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
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