I had a wierd issue in Solaris 11.3 SPARC machine. I used to check whether the port is free or not using the following method in java(returns true if port is free else returns false).
public static boolean isPortFree(int port) {
try {
new ServerSocket(port).close();
return true;
} catch (IOException e) {
System.out.println("Exception "+e.getMessage());
return false;
}
}
The above code works fine in all the OS except Solaris. In Solaris machine, when I tested with port 22, it is working as expected, but when I tested with port 12345 which is in use by another process, it is returning true which is the wrong behavior. It should throw an exception saying Address already in use and return false. Please suggest me good idea which works even in Solaris
I have figured out the problem, it is with the IPV4 and IPV6 issue in Solaris machine. C process is opening IPV4 port and the Java's code snippet
ServerSocket(port).close();
is checking on the IPV6 port. So that's the reason, it is not throwing any exception in Java even the port(IPV4) is already in use.