Java network programming 2

zhaozj2021-02-16  62

The inetaddress class inetaddress class plays an important role in the network API socket programming. Parameters pass to the flow sleeve and self-add-in socket class constructors or non-constructor methods. Inetaddress describes 32-bit or 64-bit IP addresses. To complete this feature, the iNetAddress class mainly relys on two support class INet4address and inet6address, which is inheritance relationship, inetdrress is a parent class, Inet4address and inet6address are subclasses. Since only one constructor is only one constructor, but cannot pass parameters, it is not possible to create an inetaddress object, such as the following practices are wrong:

Inetaddress IA = new inetaddress ();

However, we can create an inetaddress object or inetdress array with the following five factory methods:. GetAllByName (String Host) method Returns a reference to an inetaddress object, each object contains a separate IP address representing the corresponding hostname, this The IP address is passed through the Host parameter, and if there is no IP address if there is no IP address, this method will throw a unknownhostexception exception object. GetByAddress method Returns a reference to an inetaddress object, which contains an IPv4 address or IPv6 address. The IPv4 address is a 4-byte array. The IPv6 address is a 16-byte address array, if returned Nothing is neither 4 bytes, then the method will throw a unknownhostException exception object. GetByaddress (String Host, Byte [] addr method Returns a reference to an inetaddress object, which contains an IP address specified by the Host and 4 bytes of AddR array, or Host and 16-byte ADDR array specified The IP address, if this array is neither 4 bytes nor 16-bit bytes, then this method will throw a unknownhostException exception object. . GetByname (String Host) method Returns an inetaddress object that contains an IP address corresponding to the host specified by the Host parameter. For the specified host, if there is no IP address exists, then the method will throw a unknownhostException exception object. The getLocalHost () method returns an inetaddress object, which contains the IP address of the local machine. Considering that the local host is both a client host and a server host, in order to avoid confusion, we call the client host as a customer host, put the server The program host is called a server host. Both of the methods mentioned above refer to a reference to returning one or more inetaddress objects. In fact, each method returns a reference to one or more Inet4address / inet6address objects, and the caller does not need to know the subtypes referenced, the opposite caller The non-static method of the inetaddress object can be invoked using the returned reference, including the polymorphism of the subtype to ensure that the overload method is called. Inetaddress and its subtype object handle the transformation of the host name to the host IPv4 or IPv6 address. To complete this conversion, you need to use a domain name system. The following code demonstrates how to get the getByname method to get an inetaddress sub-class object, This object contains an IP address corresponding to the Host parameter:

Inetaddress = inetaddress.getbyName ("www.javajeff.com"))); one but get a reference to the inetaddress sub-object, you can call inetaddress's various methods to get IP address information in the inetaddress subclass object, such as Get the standard host name from the domain name service by calling getCanonicalHostName; getHostAddress () gets the IP address, getHostName () gets the host name, isloopBackAddress () determines if the IP address is a loopback address. List1 is a demonstration code: inetaddressDemo

// InetAddressDemo.javaimport java.net *;. Class InetAddressDemo {public static void main (String [] args) throws UnknownHostException {String host = "localhost"; if (args.length == 1) host = args [0]; Inetaddress IA = inetaddress.getbyname (Host); System.out.Println ("Canonical Host Name =" Ia.GetcanonicalHostName ()); System.out.Println ("Host Address =" Ia.GethostAddRESS ()); System .out.println ("Host Name =" Ia.getHostName ()); System.out.Println ("is loopback address =" };}} When there is no command line parameter, the code output is similar The following results:

Canonical Host Name = localhosthost address = 127.0.0.1host name = localhostis loopback address = true

InetaddressDemo gave you a choice for a specified host name as a command line parameter. If there is no host name being specified, use localhost (client), INetaddressDemo get a reference to the getByName (String Host) method, The standard host name, host address, host name, and IP address are the output of the LoopBack address by this reference.

转载请注明原文地址:https://www.9cbs.com/read-22604.html

New Post(0)