Java - Socket isConnected() Method
Description
The Java Socket isConnected() returns the connection state of the socket. Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket (see isClosed()) if it was successfuly connected prior to being closed.
Declaration
Following is the declaration for java.net.Socket.isConnected() method.
public boolean isConnected()
Parameters
NA
Return Value
true if the socket was successfuly connected to an address.
Exception
NA
Example 1
The following example shows the usage of Java Socket isConnected() method to get the connection status of socket instance. As first step, we've created a Socket instance using no argument constructor. Then in order to create a SocketAddress object, we've initialized an InetAddress instance of localhost address. Using InetSocketAddress object, we've created a SocketAddress object and then using bind() method, we bind the address to the socket. Once done, we're printing the connection state using isConnected() method, local port and inetaddress as shown. In the end, we closed the socket using close() method.
package com.tutorialspoint;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class SocketDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
InetAddress inetAddress=InetAddress.getByName("localhost");
SocketAddress socketAddress=new InetSocketAddress(inetAddress, 6066);
socket.bind(socketAddress);
System.out.println("Is connected: "+socket.isConnected());
System.out.println("Port number: "+socket.getLocalPort());
System.out.println("Inet Address: "+socket.getInetAddress());
socket.close();
}
}
Output
Let us compile and run the above program, this will produce the following result −
Is connected: false Port number: 6066 Inet Address: null
Example 2
The following example shows the usage of Java Socket isConnected() method to get the connection status of socket instance. As first step, we've created a Socket instance using no argument constructor. Now we're printing the connection state using isConnected() method. Then in order to create a SocketAddress object, we've initialized an InetAddress instance of localhost address. Using InetSocketAddress object, we've created a SocketAddress object and then using bind() method, we bind the address to the socket. Once done, we're again printing the connection state using isConnected() method as shown. In the end, we closed the socket using close() method.
package com.tutorialspoint;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class SocketDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
System.out.println("Is connected: "+socket.isConnected());
InetAddress inetAddress=InetAddress.getByName("localhost");
SocketAddress socketAddress=new InetSocketAddress(inetAddress, 6066);
socket.bind(socketAddress);
System.out.println("Is connected: "+socket.isConnected());
socket.close();
}
}
Output
Let us compile and run the above program, this will produce the following result −
Is connected: false Is connected: false
Example 3
The following example shows the usage of Java Socket isConnected() method to get the connection status of socket instance. As first step, we've created a Socket instance using no argument constructor. Now we're closing the socket using close() method. Then in order to create a SocketAddress object, we've initialized an InetAddress instance of localhost address. Using InetSocketAddress object, we've created a SocketAddress object and then using bind() method, we bind the address to the socket. Once done, we're printing the connection state using isConnected() method as shown. As socket is already closed, it will throw an exception. In the end, we closed the socket using close() method.
package com.tutorialspoint;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class SocketDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
socket.close();
InetAddress inetAddress=InetAddress.getByName("localhost");
SocketAddress socketAddress=new InetSocketAddress(inetAddress, 6066);
socket.bind(socketAddress);
System.out.println("Is connected: "+socket.isConnected());
socket.close();
}
}
Output
Let us compile and run the above program, this will produce the following result −
Exception in thread "main" java.net.SocketException: Socket is closed at java.base/java.net.Socket.bind(Socket.java:644) at com.tutorialspoint.SocketDemo.main(SocketDemo.java:16)
java-socket.htm