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 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 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 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 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 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 isOutputShutdown () 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 boolean isOutputShutdown () Parameter NA Return The isOutputShutdown () method would return a Boolean value 'true' if the output...
3 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 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 isBound () method of Java Socket class returns a Boolean value representing the binding state of the socket. This method would continue to return 'true' for a closed socket, if it was bound before being closed. Syntax public boolean isBound() Parameter NA Return The isBound () method would...
3 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