Modern applications often need the ability to learn information about the "hosts out" on the network. One key class in this process for Java developers is the java.net.InetAddress
. This class allows the various information about the hosts, as well as the discovering host information by different Means.
InetAddress is a deceptively simple class to use, in this it provides a simple API for working with some very complex conc Epts. For instance, it provides a standard interface for discovering IPV4 IP addresses as well as IPV6 IP addresses. In addition, it distinguishes between multicast and unicast address types transparently. Finally, there is facilities built-in for determining if a host is reachable.
Here is some useful tidbits to understand:
- If The Internet address is IPV6, the returned object from the static methods of'll be a
InetAddress
Inet6Address
object. Likewise, if the address is IPV4, the returned object from the static methods would be a Inet4Address
object.
- The IP address lookup can be
byte[]
by, in which case Highest-order byte format was used-so for the IP address 127.0.0.1
, you Would has the byte[]
{127,0,0,1}
.
- Host name resolution is goverened by caching this can be controlled by some Java system properties-from the Javadoc:
networkaddress.cache.ttl (default: -1)
Indicates the caching policy for successful name lookups from the name service. The value is specified as as an integer to indicate the number of seconds to cache the successful lookup.
A value of-1 indicates "cache Forever".
networkaddress.cache.negative.ttl (default: 10)
Indicates the caching policy for Un-successful name lookups from the name service. The value is specified as as an integer to indicate the number of seconds to cache the failure for un-successful lookups.
A value of 0 indicates "never cache". A value of-1 indicates "cache Forever".
Here is a little example class, shows some of the common techniques for using inetaddress to discover various Informat Ion
Packageorg.javalobby.tnt.net;Importjava.net.InetAddress; Public classInetaddresstest { Public Static voidMain (string[] args)throwsException {//Get by host nameInetAddress javalobby = Inetaddress.getbyname ("javalobby.org"); //Get by IP as host nameInetAddress byipasname = Inetaddress.getbyname ("64.69.35.190"); //Get by IP as Highest-order byte arrayInetAddress Byip = inetaddress.getbyaddress (New byte[] {64, 69, 35, (byte) 190}); //Get Local AddressInetAddress local =Inetaddress.getlocalhost (); //Get Local Address by Loopback IPInetAddress Localbyip = Inetaddress.getbyname ("127.0.0.1"); Printaddressinfo ("By-name (javalobby.org)", Javalobby); Printaddressinfo ("By-name (Using IP as Host)", Byipasname); Printaddressinfo ("By-ip: (64.69.35.190)", Byip); Printaddressinfo ("Special Local Host", local); Printaddressinfo ("Local Host by IP", Localbyip); } Private Static voidPrintaddressinfo (String name, inetaddress ... hosts)throwsException {System.out.println ("===== Printing Info for: '" + name + "' ====="); for(inetaddress host:hosts) {System.out.println ("Host Name:" +host.gethostname ()); System.out.println ("Canonical Host Name:" +host.getcanonicalhostname ()); System.out.println ("Host Address:" +host.gethostaddress ()); System.out.println ("Calculated Host Address:" +getipasstring (host)); System.out.print ("Is any Local:" +host.isanylocaladdress ()); System.out.print ("-Is Link Local:" +host.islinklocaladdress ()); System.out.print ("-Is Loopback:" +host.isloopbackaddress ()); System.out.print ("-Is multicast:" +host.ismulticastaddress ()); System.out.println ("-Is Site Local:" +host.issitelocaladdress ()); System.out.println ("Is Reachable in 2 seconds:" + host.isreachable (2000)); } } Private StaticString getipasstring (inetaddress address) {byte[] ipAddress =address.getaddress (); StringBuffer Str=NewStringBuffer (); for(inti=0; i<ipaddress.length; i++) { if(i > 0) str.append ('. ')); Str.append (Ipaddress[i]& 0xFF); } returnstr.tostring (); }}
Here's an example output:
===== Printing Info for: ' By-name (javalobby.org) ' =====host Name:javalobby.orgCanonical host Name:www.javalobby.orgHost Address:64.69.35.190calculated Host Address:64.69.35.190Is any Local:false-Is Link Local:false-Is Loopback:false-Is multicast:false-Is Site Local:falseis Reachable in2 seconds:true===== Printing Info for: ' By-name (Using IP as Host) ' =====host Name:www.javalobby.orgCanonical host Name:www.javalobby.orgHost Address:64.69.35.190calculated Host Address:64.69.35.190Is any Local:false-Is Link Local:false-Is Loopback:false-Is multicast:false-Is Site Local:falseis Reachable in2 seconds:true===== Printing Info for: ' By-ip: (64.69.35.190) ' =====host Name:www.javalobby.orgCanonical host Name:www.javalobby.orgHost Address:64.69.35.190calculated Host Address:64.69.35.190Is any Local:false-Is Link Local:false-Is Loopback:false-Is multicast:false-Is Site Local:falseis Reachable in2 seconds:true===== Printing Info for: ' Special Local Host ' =====Host Name:coffee-bytes-2Canonical Host Name:192.168.1.101Host Address:192.168.1.101calculated Host Address:192.168.1.101Is any Local:false-Is Link Local:false-Is Loopback:false-Is multicast:false-Is Site Local:trueis Reachable in2 seconds:true===== Printing Info for: ' Local Host by IP ' =====host Name:localhostcanonical host Name:localhosthost Address:127.0.0.1calculated Host Address:127.0.0.1Is any Local:false-Is Link Local:false-Is Loopback:true-Is multicast:false-Is Site Local:falseis Reachable in2 seconds:trueUntil Next TIME,R.J Lorimer contributing Editor-rj-at-javalobby.org Author-http://www.coffee-bytes.comSoftware consultant-http://www.crosslogic.com
General:know How to use inetaddress