InetAddress Example Program in Java

Source: Internet
Author: User

The inetaddress class has no visible constructors. To create a inetaddress object, you had to use one of the available factory methods. Factory Methods is merely a convention whereby static methods in a class return an instance of the that class. This was done in lieu of overloading a constructor with various parameter lists when have unique method names makes the R Esults much clearer. In the case of InetAddress, the three methods Getlocalhost (), getbyname (), and Getallbynam E () can is used to create instances of inetaddress. These methods is shown here:

 static   inetaddress Getlocalhost ()  throws   Unknownhostexception  static   InetAddress Getbyname (String hostName)  throws   Unknownhostexception  static   inetaddress[] Getallbyname (String hostName)  throws  Unknownhostexception 

The getlocalhost () method simply returns the inetaddress object that represents the local host. The getbyname () method returns an inetaddress for a host name passed to it. If These methods is unable to resolve the host name, they throw an unknownhostexception.

On the Internet, it's common for A and a single name to being used to represent several machines. In the world of Web servers, this is one-to-provide some degree of scaling. The getallbyname () factory method returns an array of inetaddresses, represent all of the addresses That a particular the name resolves to. It would also throw an unknownhostexception if it can ' t resolve the name to at least one address.

The following example prints the addresses and names of the local machine and both well-known Internet Web sites:

 Packageorg.javalobby.tnt.net;//demonstrate inetaddress.Importjava.net.*; Public classInetaddresstest { Public Static voidMain (String args[])throwsunknownhostexception {inetaddress Address=Inetaddress.getlocalhost ();        System.out.println (Address); Address= Inetaddress.getbyname ("starwave.com");        System.out.println (Address); InetAddress sw[]= Inetaddress.getallbyname ("www.baidu.com");  for(inti = 0; i < sw.length; i++) System.out.println (Sw[i]); Address= Inetaddress.getbyname ("www.xxdhngx.com"); }}

Here are the output produced by this program. (Of course, the output you see would be slightly different.)

default/206.148.209.138starwave.com/199.181.132.250www.baidu.com/180.97.33.108www.baidu.com/180.97.33.107www.baidu.com/61.135.169.125www.baidu.com/61.135.169.121Exception in Thread"Main"java.net.UnknownHostException:www.xxdhngx.com at Java.net.Inet6AddressImpl.lookupAllHostAddr (Native Method) at java.net.inetaddress$1.LOOKUPALLHOSTADDR (inetaddress.java:901) at Java.net.InetAddress.getAddressesFromNameService (Inetaddress.java:1293) at JAVA.NET.INETADDRESS.GETALLBYNAME0 (Inetaddress.java:1246) at Java.net.InetAddress.getAllByName (Inetaddress.java:1162) at Java.net.InetAddress.getAllByName (Inetaddress.java:1098) at Java.net.InetAddress.getByName (Inetaddress.java:1048) at Org.javalobby.tnt.net.InetAddressTest.main (Inetaddresstest.java:15)

This tutorial is a extract from the "The Complete Reference Part 2 by Herbert Schildt".

InetAddress Example Program in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.