Java Reference
In-Depth Information
/ff01:0:0:0:0:0:0:1 is a global address.
/ff01:0:0:0:0:0:0:1 is an interface-local multicast address.
$
java IPCharacteristics FF05:0:0:0:0:0:0:101
/ff05:0:0:0:0:0:0:101 is a global address.
/ff05:0:0:0:0:0:0:101 is a site wide multicast address.
$
java IPCharacteristics 0::1
/0:0:0:0:0:0:0:1 is loopback address.
/0:0:0:0:0:0:0:1 is a global address.
/0:0:0:0:0:0:0:1 is a unicast address.
Testing Reachability
The
InetAddress
class has two
isReachable()
methods that test whether a particular
node is reachable from the current host (i.e., whether a network connection can be
made). Connections can be blocked for many reasons, including firewalls, proxy servers,
misbehaving routers, and broken cables, or simply because the remote host is not turned
on when you try to connect.
public
boolean
isReachable
(
int
timeout
)
throws
IOException
public
boolean
isReachable
(
NetworkInterface
interface
,
int
ttl
,
int
timeout
)
throws
IOException
These methods attempt to use traceroute (more specifically, ICMP echo requests) to
find out if the specified address is reachable. If the host responds within
timeout
mil‐
liseconds, the methods return true; otherwise, they return false. An
IOException
will
be thrown if there's a network error. The second variant also lets you specify the local
network interface the connection is made from and the “time-to-live” (the maximum
number of network hops the connection will attempt before being discarded).
Object Methods
Like every other class,
java.net.InetAddress
inherits from
java.lang.Object
. Thus,
it has access to all the methods of that class. It overrides three methods to provide more
specialized behavior:
public
boolean
equals
(
Object
o
)
public
int
hashCode
()
public
String
toString
()
An object is equal to an
InetAddress
object only if it is itself an instance of the
InetAd
dress
class and it has the same IP address. It does not need to have the same hostname.
Thus, an
InetAddress
object for
www.ibiblio.org
is equal to an
InetAddress
object for
www.cafeaulait.org
because both names refer to the same IP address.
Example 4-7
creates
InetAddress
objects for
www.ibiblio.org
and
helios.ibiblio.org
and then tells you
whether they're the same machine.