org.eclipse.wst.server.core.util
Class SocketUtil

java.lang.Object
  extended byorg.eclipse.wst.server.core.util.SocketUtil

public class SocketUtil
extends java.lang.Object

A utility class for socket-related function. It's main purposes are to find unused ports, check whether a port is in use, and check whether a given address is a local(host) address.

Since:
1.0

Method Summary
static int findUnusedPort(int low, int high)
          Finds an unused port between the given from and to values.
static boolean isLocalhost(java.lang.String host)
          Checks if the given host (name or IP address) is pointing to the local machine.
static boolean isPortInUse(int port)
          Checks to see if the given port number is being used.
static boolean isPortInUse(int port, int count)
          Checks to see if the given port number is being used.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

findUnusedPort

public static int findUnusedPort(int low,
                                 int high)
Finds an unused port between the given from and to values.

Parameters:
low - lowest possible port number
high - highest possible port number
Returns:
an usused port number, or -1 if no used ports could be found

isPortInUse

public static boolean isPortInUse(int port,
                                  int count)
Checks to see if the given port number is being used. Returns true if the given port is in use, and false otherwise. Retries every 500ms for "count" tries.

Parameters:
port - the port number to check
count - the number of times to retry
Returns:
boolean true if the port is in use, and false otherwise

isPortInUse

public static boolean isPortInUse(int port)
Checks to see if the given port number is being used. Returns true if the given port is in use, and false otherwise.

Parameters:
port - the port number to check
Returns:
boolean true if the port is in use, and false otherwise

isLocalhost

public static boolean isLocalhost(java.lang.String host)
Checks if the given host (name or IP address) is pointing to the local machine. Although this method is not foolproof (especially if the network configuration of the current machine is incorrect or failing), it will correctly identify just about all loopback adapters and the local hostname or IP address.

This method will not attempt to make an external network connection, so it returns quickly and is safe to use in UI interfaces.

Parameters:
host - a hostname or IP address
Returns:
true if the given host is localhost, and false otherwise