Apply IP address package object in a Java2 environment
IP address package object structure
The iNetadDress object defined in the Java2 platform is a JDK API object created after the Internet IP address is encapsulated by using the Java language. 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
In this object, there is no configuration method, that is, it is not possible to create an instance of the INetadDress object using the New operator, how do you 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 function:
● 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 ()
...
As can be seen from the member method of the aboveTaddress object: In the Java application, different methods can obtain an instance of the IP address package object of the specified network object, and then call the various types of member methods defined in the object. The application provides a corresponding function.
IP address package object application instance
The parameter form of the member method defined from the aboveTadDress object can be seen that the method of obtaining the native IP address package object in the reader's Java application can only be invoked to the getLocalHost method, and the specified server can be used by the specified IP address. The name of the name creates 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, calling the member method defined in the object to obtain the service:
//IpaddressDemo.java
Import 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 ("Native IP Address:" IPINSTANCE.GETLOCALIP ());
System.out.println ("Said.com server IP address:
Ipinstance.getccidnetip ("www.ccidnet.com.cn"))));
System.out.Println ("Said.com server domain name:"
ipinstance.getccidnetName ());
}
/ / Get the native IP address member method
Public inetaddress getLocalip ()
{
Try
{
Localipaddress = inetaddress.getlocalhost ();
Catch (UnknownHOSTEXCEPTION E)
{}
Return (Localipaddress);
}
/ / Get the IP address member method of the CCID network server
Public inetaddress getccidnetip (String strservername)
{
Try
{
CCIDNetipaddress = inetaddress.getbyName (strServername);
}
Catch (UnknownHOSTEXCEPTION E)
{}
Return (CCIDNetipaddress);
}
// obtain computer system domain name member method through IP address package object
Public String getccidnetName ()
{
String strservername = null;
Try
{
STRSERVERNAME = ccidnetipaddress.getHostName ();
}
Catch (SecurityException E)
{}
Return (strservername);
}
}
In the above program code, the three member methods of getLocalip, getccidnetip, and getcidnetDemo are defined in Java Main Method, which is used to obtain IP address objects to the current computer system. Get the IP address object to the CCID network server and use the address. Object Gets the method of server domain names. 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. The output result of the program is:
This machine IP address: Support / 192.168.19.251
CCID network server IP address: www.ccidnet.com.cn/210.51.0.73
CCID network server domain name: www.ccidnet.com.cn
summary
A variety of types of objects are defined in the Java2 platform, which is used to implement network communication and data exchange of Java programs in URL, Socket, RMI, and CORBA, and this article is only a Java package for basic application objects in Internet environments. The structure of the object and the form of the member method have made a brief analysis, and a simple example program describes the application of its membership methods, hoping to use the Java language to make network programming to help readers.