Java Socket connect() method25 Mar 2025 | 3 min read connect(SocketAddress endpoint)The connect() method of Java Socket class connects the specified socket to the server. SyntaxParameterThe parameter 'endpoint' represents the SocketAddress. ReturnNA ThrowsIOException - if an error occurs during the connection. IllegalBlockingModeException - if this socket has an associated channel and the channel is in non-blocking mode. IllegalArgumentException - if the specified endpoint is null or if the SocketAddress subclass not supported by this socket. Example 1Output: Inet address: localhost/127.0.0.1 Port number: 1085 Example 2Output: Exception in thread "main" java.lang.IllegalArgumentException: port out of range:-1085 at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:143) at java.net.InetSocketAddress. Example 3Output: Exception in thread "main" java.lang.IllegalArgumentException: connect: The address can't be null at java.net.Socket.connect(Socket.java:560) at java.net.Socket.connect(Socket.java:538) at com.tpointtech.JavaSocketConnectExample3.main(JavaSocketConnectExample3.java:13) connect(SocketAddress endpoint, int timeout)The connect() method of Java Socket class connects this socket to the server with the given timeout value. A zero timeout is inferred as an infinite timeout. SyntaxParameterendpoint - it represents the SocketAddress. timeout - it represents the timeout value to be used in milliseconds. ReturnNA ThrowsIOException - if an error occurs during the connection. SocketTimeoutException - if timeout expires before connecting. IllegalBlockingModeException - if this socket has an associated channel and the channel is in non-blocking mode. IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket. Example 1Output: Inet address: localhost/127.0.0.1 Port number: 1085 Example 2Output: Exception in thread "main" java.lang.IllegalArgumentException: connect: timeout can't be negative at java.net.Socket.connect(Socket.java:563) at com.tpointtech.JavaSocketConnectExample5.main(JavaSocketConnectExample5.java:19) 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 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 getReceiveBufferSize() method of Java Socket class returns the buffer size (SO_RCVBUF option) for this socket. Syntax public int getReceiveBufferSize () throws SocketException Parameter NA Returns The getReceiveBufferSize() method returns the buffer size or the SO_RCVBUF option for this socket. Throws SocketException - if there is an error in the underlying...
3 min read
Java Socket isClosed () method The isClosed () method of Java Socket class returns the closed state of the socket. Syntax public boolean isClosed () Parameter NA Return The isClosed () method would return a Boolean value 'true' if the socket has been closed successfully. Example 1 import java.io.IOException; import java.net.*; public class JavaSocketIsClosedExample1 { ...
1 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 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 bind() method of Java Socket class binds the socket to a local address. If the specified address is null, then the system will automatically pick up a port number and a valid local address to bind with the socket. Syntax public void bind(SocketAddress bindpoint)...
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 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
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