Java Network Programming notes (2)

Source: Internet
Author: User
Tags array array length object

Java Network Programming Notes


Two find Internet addresses
Chapter 6 looking up Internet Addresses

The class is a Java encapsulation of IP addresses, which is used by most other network classes, including Socket,serversocket,url,datagramsocket,datagrampacket.
This class contains hostname and address, but is not public.

1 Getting InetAddress objects
The InetAddress class does not have a public constructor, and the InetAddress object is available through three static methods:
public static inetaddress Inetaddress.getbyname (String hostName) throws Unknownhostexception
public static inetaddress[] Inetaddress.getallbyname (String hostName) throws Unknownhostexception
public static inetaddress Inetaddress.getlocalhost () throws Unknownhostexception
These three methods make a network connection to get all the information they need. Other methods in the class, such as GetAddress () and GetHostName (), use the information provided by these 3 methods without network connectivity, but in very small cases they also connect to the network without throwing any exceptions.

The parameter hostname can be a URL such as a or a dotted IP address such as a string
After Java1.1, if you use an IP address to do the parameter, you will create a Inetadress object based on this IP address instead of using a DNS check. This may create a InetAddress object that does not exist on the host. The InetAddress object created using this method, the host name (HostName), is initialized to the dot-IP address. DNS checks the actual host name only if the hostname is requested (as shown by calling GetAddress () and implicitly as called ToString ()), and if the DNS check finds that the specified IP address is not found, then hostname remains as the original dot IP address. And not throw unknownhostexception
Hostname than IP address stability, for a host hostname is often the same as the IP address may change, so it is best to use hostname to do parameters, and only when the host does not have hostname to use the dot-IP address.

Some hosts have more than one IP address, and use Getallbyname () to get all the IP addresses of a host name in the form of an array.

Getlocalhost () Gets the InetAddress object for this machine

2 Getting Information
Public String gethostname ()
The hostname can be obtained, especially when the InetAddress object is obtained by using the dot-IP address as a parameter, and the GETHOSTNAME is hostname.

Public String gethostaddress ()
Get the dot-IP address string, which can be used to print out IP addresses

Public byte[] GetAddress ()
Get an IP address, stored in a byte array, such as the IP address in IPv4 format will get an array of length 4 IP, then ip[0]=10,ip[1]=2,ip[2]=3,ip[3]=4; if IPv6, then the array length is not 4.
Note that the returned byte should be unsigned, 0~255, but Java byte is signed. BYTE greater than 127 is treated as a negative number. Therefore, you should use INT (or short, etc.) to store every digit of the IP address and adjust it accordingly.
int unsignedbyte = Signedbyte < 0? Singedbyte + 256:singedbyte;

public boolean equals (Object O)
The condition that an object equals to a InetAddress object is that the object is an instance of the InetAddress class and that they have the same IP address. equals do not need the same hostname.

4 routine

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.