This discussion is archived
3 Replies Latest reply: Aug 10, 2012 8:54 PM by EJP RSS

socket bind fails when port in use as rootuser but succeeds as nonroot user

542029 Newbie
Currently Being Moderated
Hi All,

I have a prerequiste to check if a port is busy/already bound on Solaris 11.

java version : java version "1.6.0_24"
os : SunOS 5.11 11.0 i86pc i386 i86pc

The code snippet is as follows.
=====================================================================================
arrayOfInetAddress = InetAddress.getAllByName("example.com");
try
{
for (int j = 0; j < arrayOfInetAddress.length; j++)
{
System.out.println(" Trying for host : " + arrayOfInetAddress[j] + " and port : " + i);
localServerSocket = new ServerSocket(i, 0, arrayOfInetAddress[j]);
localServerSocket.close();
System.out.println("Port = " + i + " passed by the user is free");
}
}
catch (IOException localIOException)
{
System.out.println("isPortFree:Port=" + i + " is busy. Exception ioe=" + localIOException.getMessage());
localIOException.printStackTrace();
}


====================================================================

when i run this application as a root user it correctly complains saying the port is already bound

java CheckFreePort 9876
java.net.BindException: Address already in use

but when i run it as a non root user it says the port is free which is incorrect.
java CheckFreePort 9876
Port = 9876 passed by the user is free

what could be the reason that bind succeeds even though the port is in use as a non root user.

thanks,
Prakash

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points