by, 04-25-2012 at 10:27 PM (580 Views)
Certain methods are provided by this class to access the IP addresses & hostnames.
InetAddress objects are created by using these methods.
Static InetAddress getLocalHost() throws UnknownHostException For the local machine, InetAddress is returned by this method.
Static InetAddress getByName(String host) throws UnknownHostException For specified hostname, InetAddress object is returned by this method. Hostname could either be an IP like 126.96.36.199 or pneumonic identifier like “www.Java.com” Client can only use this method to obtain the details of remote hosts.
From InetAddress object, information is extracted by using these methods.
byte getAddress() An array of bytes is returned by this method corresponding to the IP address, that is present in the InetAddress object. The array is in high byte first or network byte order and should be converted into a string, before it is displayed to the screen.
String getHostName()Host name is returned which is present in InetAddress object. If unknown host name is present, efforts are made to look up, however if it doesn’t succeed then as a string IP addresses are returned.
UnknownHostException This is IOException subclass which shows that the host name is not identified successfully.
SecurityException When Java security manager restricts the actions to take place, this kind of error is being thrown. Currently, an InetAddress object might be created by Applet for the host from which it originated. For another host, creation of InetAddress object will be throwing this exception.