public class NetworkUtilities
extends java.lang.Object
The access point used by these methods is implementation dependent.
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getByName(java.lang.String host)
Determines the IP address of a host, given the host's name.
|
static java.lang.String |
getCanonicalHostName(java.lang.String host)
Gets the fully qualified domain name for this host.
|
static boolean |
isReachable(java.lang.String host,
int ttl,
int timeout)
Test whether that host is reachable.
|
public static java.lang.String getByName(java.lang.String host) throws java.net.UnknownHostException
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its
IP address. If a literal IP address is supplied, only the
validity of the address format is checked.
The IP Address must be
a literal IPv4 address or an IP-literal
as specified by
RFC 3986.
An IP-literal
requires to surround
the IPv6Address
with square brackets ('[', ']'),
IPvFuture
addresses from rfc3986 are not supported.
If the host is null then an IP address representing an address of the loopback interface is returned. See RFC 3330 section 2 and RFC 2373 section 2.5.3.
If the device supports both IPv4 and IPv6 addresses, it is implementation dependent whether an IPv4 or IPv6 address is returned.
This method is subject to a security check to verify that the caller is allowed to establish a socket connection. See package description for details. If the caller lacks the necessary permission, the method throws SecurityException.
host
- the specified host, or null
.java.net.UnknownHostException
- if no IP address for the
host
could be found.java.lang.SecurityException
- if a security manager exists
and its security policy doesn't allow the operation.public static java.lang.String getCanonicalHostName(java.lang.String host) throws java.lang.IllegalArgumentException, java.net.UnknownHostException
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its
IP address.
If an IP address is provided, it must be
a literal IPv4 address or an IP-literal
as specified by
RFC 3986.
An IP-literal
requires to surround
the IPv6Address
with square brackets ('[', ']'),
IPvFuture
addresses from rfc3986 are not supported.
This method is subject to a security check
to verify that the caller is allowed to know
the host for this IP address, i.e., to connect to the host.
See package description for details.
If the caller lacks the necessary permission,
the method throws SecurityException.
If the operation is allowed by the security policy but the provided host
(name or IP address) is not resolvable to a fully qualified domain name
this method throws an UnknownHostException
.
host
- a host name or IP addressjava.lang.NullPointerException
- if host
is null
.java.net.UnknownHostException
- if the host cannot be resolved.java.lang.IllegalArgumentException
- if the IP Address is not valid.java.lang.SecurityException
- if a security manager exists
and its security policy doesn't allow the operation.public static boolean isReachable(java.lang.String host, int ttl, int timeout) throws java.io.IOException
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
The host name can either be a machine name, such as
"java.sun.com
", or a textual representation of its
IP address.
The textual representation of the IP Address must be
a literal IPv4 address or an IP-literal
as specified by
RFC 3986.
An IP-literal
requires to surround
the IPv6Address
with square brackets ('[', ']'),
IPvFuture
addresses from rfc3986 are not supported.
The ttl
parameter
let the caller specify the maximum number of hops the packets should go through.
The ttl
parameter is applicable only for ICMP ECHO requests
and is ignored on TCP connections to port 7 (Echo).
A negative value for the ttl
will result in an
IllegalArgumentException being thrown.
The timeout value, in milliseconds, indicates the maximum amount of time the try should take. If the operation times out before getting an answer, the host is deemed unreachable. A negative value will result in an IllegalArgumentException being thrown.
This method is subject to a security check to verify that the caller is allowed to establish a socket connection. See package description for details. If the caller lacks the necessary permission, the method throws SecurityException.
host
- a host name or IP addressttl
- the maximum numbers of hops to try or 0 for the
defaulttimeout
- the time, in milliseconds, before the call abortsboolean
indicating if the address is reachable.java.lang.NullPointerException
- if host
is null
.java.lang.IllegalArgumentException
- if either timeout
or ttl
are negative.java.io.IOException
- if a network error occursjava.lang.SecurityException
- if a security manager exists
and its security policy doesn't allow the operation.Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. Use of this specification is subject to license terms.