This question is almost similar to this previous question Sockets: Discover port availability using Java. But somehow its not working properly when I check the derby listening port.
public static boolean isPortavailable(int port){
ServerSocket ss = null;
DatagramSocket ds = null;
try{
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
}
catch(IOException e){
}
finally{
if(ss != null){
try{
ss.close();
ds.close();
}
catch(IOException e){
/* should not be thrown */
}
}
}
return false;
}
Here is my main method
public static void main(String[] args){
System.out.println("Derby:"+isPortAvailable(1527));
System.out.println("Jetty:"+isPortAvailable(7080));
System.out.println("Tomcat:"+isPortAvailable(8084));
}
I started services with the port for derby 1527, jetty 7080 and tomcat 8084. when I run this main method it gives the following result
Derby:true
Jetty:false
Tomcat:false
I am unable to find why Its giving wrong answer for derby, can anyone help. Thanks