The INetAddress object defined in the IP address package object is the JDK API object created after the Internet IP address is created using the Java language for the network communication program design process. The INet4Address and Inet6Address objects are also defined in the Java2 platform with an inetaddress object, respectively correspond to an Internet address object that complies with IPv4 specifications and IPv6 specifications, respectively. The definition inheritance of the inetaddress object is as follows: java.LANG.Object | - java.net.inetadDress does not define a constructor in this object, that is, how to create an instance of the InetadDress object using the New operator, then how to get the instance of the object ? A variety of types of member methods are defined in the inetaddress object, which can call these methods in the application to implement the corresponding network features: ● Byte [] getaddress () ● static inetaddress [] getAllByName (String host) ● static inetaddress getByaddr byte [] addr) ● static InetAddress getByAddr (String host, byte [] addr) ● static InetAddress getByName (String host) ● String getHostAddress () ● static InetAddress getLocalHost () ... ... can be seen from the member method defined name above InetAddress object Out: In the Java application, you can use different ways to get the IP address package object instance of the specified network object, and then call the various types of member methods defined in the object to provide the application to the application. IP Address Package Object Application Example You can see the parameters of the member method defined by the aboveTadDress object. The method of obtaining a native IP address package object in the reader's Java application is only required to call the getLocalHost method, and can be adopted Specify an IP address, specify the server name to create an IP address package object to other computer systems.
The following instance code demonstrates the creation method of the inetaddress object instance, and after creating an IP address package object instance, call the member method defined in the object to obtain the service: //ipaddressDemo.javaimport java.net. *; Public Class ipaddressDemo {InetAddress localIPAddress = null; InetAddress ccidnetIPAddress = null; public static void main (String args []) {IPAddressDemo ipInstance; ipInstance = new IPAddressDemo (); System.out.println ( "Local IP address:" ipInstance.getLocalIP ( )); System.out.println ("CCID network server IP address:" ipinstance.getccidnetip ("www.ccidnet.com.cn")); System.out.Println ("Said.com server domain name:" ipInstance.getCCIDNETName ());} // obtain the IP address of a member of methods public InetAddress getLocalIP () {try {localIPAddress = InetAddress.getLocalHost ();} catch (UnknownHostException e) {} return (localIPAddress);} // obtain SEOUL members public IP address of the server method InetAddress getCCIDNETIP (String strServerName) {try {ccidnetIPAddress = InetAddress.getByName (strServerName);} catch (UnknownHostException e) {} return (ccidnetIPAddress);} // IP address of an encapsulated object by obtaining Computer system domain name member method public string getccidnetName () {string strservername = null; try {strservername = ccidnetipaddress.getHostName ();} CATCH (SECURITYEXCEPTION E) {} Return (STRSERVERNAME);}} The three member methods of getLocalIP, getcidnetip, and getccidnetName are defined in Java Main Method IpadDressDemo, which are used to obtain the current computer system. IP address object, get the IP address object to the CCID network server and use the address object to obtain the server domain name. From the definition of these members, you can see that the static type member method defined in the inetaddress object getLocalHost and getByname returns the inetaddress object instance. In the subsequent program code, the instance member method can be called directly to obtain the service, for example in the getccidnetName method. The process of calling member methods gethostname.