Java network programming learning notes (1)

xiaoxiao2021-03-06  40

The first part looks for Internet addresses

The Java.Net.ineTaddress class is the Java IP address package class.

One. Inetaddress has three static methods suitable for initializing the InetadDress object, which are: 1) Public static inetaddress inetaddress.getbyName (String hostname) Throws unknowhostexception It needs to be viewed as a parameter, and use DNS to find the host's IP address 2) Public static inetaddress [] inetaddress.getAllByname (String Hostname) Throws unknowhostException Some computers have more than one Internet address, given a host name, inetaddress.getAllByname () returns an array containing all addresses corresponding to the host name . For example: InetAddress [] address = InetAddress.getAllByName ( "www.apple.com") 3) public static InetAddress InetAddress.getLocalHost () throws UnknowHostException InetAddress.getLocalHost () is executed on a computer, the machine will return InetAddress.

Several other methods for the Java.net package can also return an InetadDress object.

two. The inetaddress class contains three methods that return the host name as a string and return the IP address as a string and the byte array: PUBLIC STRING GETHOSTNAME () getHostName () method Returns a string, the string contains Host name with the IP address represented by the inetaddress object, if there is no host name to return a digital IP address with a point-in-point form. 2) Public Byte [] getaddress () getAddress () returns the IP address in a network byte order as a byte array. For example: inetaddress me = inetaddress.getlocalhost ();

Byte [] address = me.getaddress ();

The byte returned by the getaddress () is unsigned, Java has no basic data type with a symbolic byte. The bytes of more than 127 or more will be treated negatively. So you need to do the following improvements: int unsignedbyte = signedByte <0? SignedByte 256: SignedByte;

3) Public string gethostaddress () gethostaddress () method Returns a string that contains IP addresses in dichotomics.

There is no SethostName () and setaddress () methods in the inetaddress class, so Java ensures that host names and IP addresses.

three. The Java.Net.ientAddress class inherited java.lang.object. This class can access all methods in java.lang.object. It overloads three methods and provides a more special feature 1) Public Boolean Equals (Object O) This object is equivalent to an inetaddress object only when the object itself is an instance of the InetadDress class and it has the same IP address. However, there is no need to have the same hostname. That is, as long as the IP address is the same. 2) Public int.come () method Returns an int when the inetaddress object is used as a keyword as a keyword. The INT returned by hashcode () is just the 4 bytes of the converted int. This value is different for every two inetaddress objects. If the two inetaddress objects have the same address, even if they have different hostnames, they also have the same hash code. Therefore, use the equivalent inetaddress object as a keyword to store two objects in the hash table, the second object overloads the first object. The solution is to use the string returned using the gethostname () method as the keyword instead of the inetaddress itself. 3) Public string toString () TOSTRING () The resulting string has the following form: Hostname / Dimension Address is not all INetAddress objects with host name, no host name uses point-in-point IP address instead, The format is not explicitly called toString (). To call, the syntax is simple: inetaddress thiscomputer = inetaddress.getlocalhost (); string address = thisComputer.tostring ();

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

New Post(0)