net.sf.jabb.util.net
Class SocketUtility

java.lang.Object
  extended by net.sf.jabb.util.net.SocketUtility

public class SocketUtility
extends Object

Utility for Socket related functions.

Author:
James Hu

Constructor Summary
SocketUtility()
           
 
Method Summary
static int getFreeServerPort()
          Get a free server port that can be used.
static int getFreeServerPort(boolean tryOthers, int... ports)
          Get a free server port that can be used.
static int getFreeServerPort(int... ports)
          Get a free server port that can be used.
static int getFreeServerPort(int port)
          Get a free server port that can be used.
static boolean isServerPortFree(int port)
          Check whether the specified server socket port is free or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketUtility

public SocketUtility()
Method Detail

isServerPortFree

public static boolean isServerPortFree(int port)
Check whether the specified server socket port is free or not. Be aware that this method is not safe if there are so many programs trying to open server sockets.

Parameters:
port - the port number to be checked
Returns:
true if the port is free, or false if it is being used by other program.

getFreeServerPort

public static int getFreeServerPort(int port)
Get a free server port that can be used. A preferred port number can be specified. Be aware that this method is not safe if there are so many programs trying to open server sockets.

Parameters:
port - preferred port number, or zero if no preference.
Returns:
the preferred port or any other free port, or zero if any exception occurred.

getFreeServerPort

public static int getFreeServerPort()
Get a free server port that can be used. Be aware that this method is not safe if there are so many programs trying to open server sockets.

Returns:
any free port, or zero if any exception occurred.

getFreeServerPort

public static int getFreeServerPort(boolean tryOthers,
                                    int... ports)
Get a free server port that can be used. Several preferred port numbers can be specified.

Parameters:
tryOthers - If all preferred ports are occupied, try other free ports or not.
ports - preferred port numbers
Returns:
A free port number, or zero if not found or exception occurred.

getFreeServerPort

public static int getFreeServerPort(int... ports)
Get a free server port that can be used. Several preferred port numbers can be specified. If all preferred ports are occupied, other free ports will be tried.

Parameters:
ports - preferred port numbers
Returns:
A free port number, or zero if not found or exception occurred.


Copyright © 2012. All Rights Reserved.