InetAddress class to create a new InetAddress object

Source: Internet
Author: User

The Java.net.InetAddress class is a high-level representation of Java for IP addresses, including IPV4 and IPV6. Most other network classes use this class, including Socket,serversocket,url,datagramsocket,datagrampacket. Generally speaking, it includes a host name and an IP address.

Create a new InetAddress object

The InetAddress class does not have a public constructor. In fact, InetAddress has some static factory methods that can be connected to a DNS server to resolve host names. The most common is inetaddress.getbyname (). For example, you can find www.oreilly.com as follows:

InetAddress address = Inetaddress.getbyname ("www.oreilly.com");

This method does not just set a private string field in the InetAddress class. In fact, it will establish a connection to the local DNS server to find the name and number of the address (if you have previously looked up this host, this information may be cached locally, if so, there is no need to establish a network connection). If the DNS server cannot find this address, this method throws a Unknownhostexception exception, which is a subclass of IOException.

The following example shows a complete program that creates a InetAddress object for www.oreilly.com, which includes all the necessary import and exception handling:

package o2;import java.net.inetaddress; import java.net.unknownhostexception;public class oreillybyname {     Public static void main (String[] args)  {         try {            InetAddress  Address = inetaddress.getbyname ("www.oreilly.com");             system.out.println (Address.gethostaddress ());         } catch  (unknownhostexception e)  {             e.printstacktrace ();        }     }} 

We mentioned before that www.oreilly.com actually has two addresses. Getbyname () returns which address is indeterminate. If for some reason you need to get all the addresses of a host, you can call Getallbyname () and it will return an array:

Package o2;import java.net.inetaddress;import java.net.unknownhostexception;public class  oreillybyname {    public static void main (String[] args)  {        try {             inetaddress[] addrs = inetaddress.getallbyname ("www.oreilly.com") ;             if (addrs != null & & addrs.length > 0) {                 for (Inetaddress addr : addrs) {                     system.out.println ( Addr.gethostaddress ());                 }            }        }  catch  (unknownhostexception e)  {             e.printstacktrace ();        }     }}

Finally, the Getlocalhost () method returns a InetAddress object for the host running the code:

InetAddress me = Inetaddress.getlocalhost ();

This method attempts to connect to DNS to get a real hostname and IP address, such as "elharo.laptop.corp.com" and "192.1.254.68", but if it fails, it returns the loopback address, which is the hostname "localhost" and the dot-four-segment address " 127.0.0.1 ". Find an example of a local machine address:

Package O2;import Java.net.inetaddress;import Java.net.unknownhostexception;public class MyAddress {public static void            Main (string[] args) {try {inetaddress address = inetaddress.getlocalhost ();        System.out.println (Address.gethostaddress ());        } catch (Unknownhostexception e) {e.printstacktrace (); }    }}


InetAddress class to create a new InetAddress object

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.