public class NetUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
NetUtils.PortAvailability |
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_PORT |
| Modifier and Type | Method and Description |
|---|---|
static NetUtils.PortAvailability |
checkPort(int portNumber)
There are 4 possibilities when you want to setup a server socket on a port:
1.
|
static String |
getCanonicalHostName()
This method returns the fully qualified name of the host.
|
static Socket |
getClientSocket(String host,
int port,
int msecTimeout) |
static int |
getFreePort()
Gets a free port at the time of call to this method.
|
static int |
getFreePort(String hostName,
int startingPort,
int endingPort)
Returns a random port in the specified range
|
static InetAddress[] |
getHostAddresses() |
static String[] |
getHostIPs() |
static String |
getHostName() |
static int |
getNextFreePort(String hostName,
int port)
Get the next free port (incrementing by 1)
|
static byte[] |
ip2bytes(String ip) |
static boolean |
isEqual(String host1,
String host2)
Painfully thorough error-handling.
|
static boolean |
isLocal(String ip) |
static boolean |
isLocalHost(String ip) |
static boolean |
isPortFree(int portNumber) |
static boolean |
isPortFree(String hostName,
int portNumber) |
static boolean |
isPortStringValid(String portNumber) |
static boolean |
isPortValid(int portNumber) |
static boolean |
isRemote(String ip) |
static boolean |
isRunning(int port)
convenience method for the local machine
|
static boolean |
isRunning(String host,
int port) |
static boolean |
isRunning(String host,
int port,
int timeoutMilliseconds)
There is sometimes a need for subclasses to know if a
local domain is running. |
static boolean |
isSecurePort(String hostname,
int port)
This method accepts a hostname and port #.
|
static boolean |
isThisHostLocal(String hostname)
Return true if hostname represents the current machine.
|
static void |
main(String[] args) |
static String |
trimIP(String ip) |
public static final int MAX_PORT
public static boolean isThisHostLocal(String hostname)
public static boolean isEqual(String host1, String host2)
host1 - host2 - public static String getHostName()
public static String getCanonicalHostName() throws UnknownHostException
UnknownHostException - so it can be handled on a case by case basispublic static InetAddress[] getHostAddresses()
public static String[] getHostIPs()
public static byte[] ip2bytes(String ip)
public static boolean isLocalHost(String ip)
public static boolean isLocal(String ip)
public static boolean isRemote(String ip)
public static int getNextFreePort(String hostName, int port)
hostName - The host name on which the port is to be obtainedport - The port numberpublic static int getFreePort(String hostName, int startingPort, int endingPort)
hostName - The host on which the port is to be obtained.startingPort - starting port in the rangeendingPort - ending port in the rangepublic static boolean isPortValid(int portNumber)
public static boolean isPortStringValid(String portNumber)
public static boolean isPortFree(String hostName, int portNumber)
public static NetUtils.PortAvailability checkPort(int portNumber)
portNumber - public static boolean isPortFree(int portNumber)
public static int getFreePort()
0 .
Note that this method guarantees the availability of the port only at the time of call. The method does not bind to this port.
Checking for free port can fail for several reasons which may indicate potential problems with the system. This method acknowledges the fact and following is the general contract:
0
is returned, indicating that the method failed for some reasons and
the callers should take the corrective action. (The method need not
always throw an exception for this).
public static boolean isSecurePort(String hostname, int port) throws IOException, ConnectException, SocketTimeoutException
hostname - - String name of the host where the connections has to be madeport - - int name of the port where the connections has to be madeIOException - if an error occurs during the connectionConnectException - if an error occurred while attempting to connect a socket to a remote address and port.SocketTimeoutException - if timeout(4s) expires before connectingpublic static boolean isRunning(String host, int port, int timeoutMilliseconds)
local domain is running. An example of such a command is
change-master-password command. The stop-domain command also needs to
know if a domain is running without having to provide user
name and password on command line (this is the case when I own a domain
that has non-default admin user and password) and want to stop it
without providing it.
In such cases, we need to know if the domain is running and this method provides a way to do that.
the - timeout in millisecondspublic static boolean isRunning(String host, int port)
public static final boolean isRunning(int port)
public static void main(String[] args)
Copyright © 2017–2020 Eclipse Foundation. All rights reserved.