Java - Socket isBound() Method
Description
The Java Socket isBound() returns the binding state of the socket. Closing a socket doesn't clear its binding state, which means this method will return true for a closed socket (see isClosed()) if it was successfuly bound prior to being closed.
Declaration
Following is the declaration for java.net.Socket.isBound() method.
public boolean isBound()
Parameters
NA
Return Value
true if the socket was successfuly bound to an address.
Exception
NA
Example 1
The following example shows the usage of Java Socket isBound() method to get the binding 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 binding state using isBound() 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 Bound: "+socket.isBound());
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 Bound: true Port number: 6066 Inet Address: null
Example 2
The following example shows the usage of Java Socket isBound() method to get the binding status of socket instance. As first step, we've created a Socket instance using no argument constructor. Now we're printing the binding state using isBound() 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 binding state using isBound() 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 Bound: "+socket.isBound());
InetAddress inetAddress=InetAddress.getByName("localhost");
SocketAddress socketAddress=new InetSocketAddress(inetAddress, 6066);
socket.bind(socketAddress);
System.out.println("Is Bound: "+socket.isBound());
socket.close();
}
}
Output
Let us compile and run the above program, this will produce the following result −
Is Bound: false Is Bound: true
Example 3
The following example shows the usage of Java Socket isBound() method to get the binding 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 binding state using isBound() 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 Bound: "+socket.isBound());
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)