Java Reference
In-Depth Information
Here, signedByte may be either positive or negative. The conditional operator ? tests
whether signedByte is negative. If it is, 256 is added to signedByte to make it positive.
Otherwise, it's left alone. signedByte is automatically promoted to an int before the
addition is performed, so wraparound is not a problem.
One reason to look at the raw bytes of an IP address is to determine the type of the
address. Test the number of bytes in the array returned by getAddress() to determine
whether you're dealing with an IPv4 or IPv6 address. Example 4-5 demonstrates.
Example 4-5. Determining whether an IP address is v4 or v6
import java.net.* ;
public class AddressTests {
public static int getVersion ( InetAddress ia ) {
byte [] address = ia . getAddress ();
if ( address . length == 4 ) return 4 ;
else if ( address . length == 16 ) return 6 ;
else return - 1 ;
}
}
Address Types
Some IP addresses and some patterns of addresses have special meanings. For instance,
I've already mentioned that 127.0.0.1 is the local loopback address. IPv4 addresses in
the range 224.0.0.0 to 239.255.255.255 are multicast addresses that send to several sub‐
scribed hosts at once. Java includes 10 methods for testing whether an InetAddress
object meets any of these criteria:
public boolean isAnyLocalAddress ()
public boolean isLoopbackAddress ()
public boolean isLinkLocalAddress ()
public boolean isSiteLocalAddress ()
public boolean isMulticastAddress ()
public boolean isMCGlobal ()
public boolean isMCNodeLocal ()
public boolean isMCLinkLocal ()
public boolean isMCSiteLocal ()
public boolean isMCOrgLocal ()
The isAnyLocalAddress() method returns true if the address is a wildcard address ,
false otherwise. A wildcard address matches any address of the local system. This is
important if the system has multiple network interfaces, as might be the case on a system
with multiple Ethernet cards or an Ethernet card and an 802.11 WiFi interface. In IPv4,
the wildcard address is 0.0.0.0. In IPv6, this address is 0:0:0:0:0:0:0:0 (a.k.a. ::).
Search WWH ::




Custom Search