Network programming – Internet query in Java

premise

In order to deeply understand the concepts such as URL and URI, or learn some socket related knowledge, it is necessary to systematically understand some basic knowledge related to the Internet.

Internet address

A device connected to the Internet (Internet) is called a node, and any computer node is called a host. Each node or host is identified by at least one unique number, which is called Internet address or IP address.

IP and domain name

If we use Java as the development language, we don't need to worry about the working principle of IP or domain name, but we need to understand some basic knowledge of IP addressing. At present, the commonly used networks are IPv4 networks. Each computer node is identified by a 4-byte (32bit) digital ID. the format of this digital ID is dot Quad (in the form of XXX. XXX. XX. XX). Each number is an unsigned word section, and the value range is 0 to 255. When data is transmitted through the network, the header of the packet shall include the machine address (destination address) to which the packet is sent and the machine address (source address) to which the packet is sent.

The total number of IPv4 type IP addresses that can be used is about more than 4 billion, so it is impossible to assign a unique IPv4 IP address to everyone on the earth, so IPv6 was born. At present, the network is transitioning from IPv4 to IPv6 (but the transition process is relatively slow and there are many factors). IP addresses in IPv6 networks are identified by 16 byte (128bit) numbers. IPv6 addresses are usually represented in 8 areas separated by English colons, and each area is 4 hexadecimal digits. For example, fedc: ba98:7654:3210: fedc: ba98:7654:3210 is a legal IPv6 address. In the hybrid network of IPv4 and IPv6, the last four bytes of IPv6 address are sometimes expressed as the dot divided four segment address of IPv4 address. IPv6 address is only available in jdk1 4 and later versions support, in other words, jdk1 3 or earlier versions can only use IPv4 addresses.

Although the computer can easily process numbers, the memory of the human brain is not sensitive to numbers. Therefore, a domain name system (DNS) has been developed to convert the host name easy to remember by the human brain (such as www.baidu. Com) into a digital Internet address (such as 183.232.231.173). The specific content of DNS is not expanded here. As a developer, we can simply understand it as a huge distributed database for mapping host names (domain names) and IP addresses. Draw a diagram as follows:

port

Because each computer does not do only one thing, it is equivalent to that each business logic done by the computer needs to be logically isolated. For example, the processing of FTP request should be separated from the processing of e-mail, and the processing of FTP request should also be separated from the processing of web business. Therefore, each business logic needs to use a logically separated ID, which is port. Generally, each computer has thousands of logical ports (specifically, each transport layer protocol has 65535 ports. In Windows system, port 11023 is the system port, which cannot be modified by the user, port 102465534 is the reserved port for the user, and port 65535 is the reserved port for the system). Each port can be assigned to a specific service. For example, HTTP protocol, the underlying protocol of the web, generally uses port 80 for communication. If you use the browser URL to access port 80 of the server, you can omit the port number in the URL.

Java abstraction of network

InetAddress

The word InetAddress is an acronym for Internet address and represents an Internet address. java. net. The InetAddress class is a highly abstract representation of IP addresses (including IPv4 and IPv6 addresses) in Java. Most classes related to network programming will use InetAddress, such as socket and ServerSocket. The two core attributes of InetAddress are host name and IP address, and the corresponding attributes are hostname and address.

Create InetAddress instance

Creating an InetAddress instance mainly depends on its factory method (in fact, the constructor of InetAddress is package private, that is, the instance cannot be created through the new keyword). A commonly used static factory method is:

static InetAddress getByName(String host) throws UnkNownHostException

The parameter can be a host name (domain name) or a four segment address. The former is equivalent to finding a connectable IP address through the host name, and the latter is equivalent to backchecking the host name through the IP address. It is worth noting that the use of this method call will establish a connection with the local DNS server to find the host name or digital address, If the DNS server cannot find the host or address, it will throw an unknownhostexception exception.

	public static void main(String[] args) throws Exception{
		InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
		System.out.println(inetAddress);
	}

InetAddress overrides the toString method, and the returned result is in the format of hostname / address. One possible result of the above main method is:

www.baidu.com/14.215.177.39

Sometimes, if we know the digital IP address, we can directly create an InetAddress instance from the digital address, so we don't have to use the getbyname (string host) method to interact with DNS.

static InetAddress getByAddress(byte[] addr)throws UnkNownHostException
static InetAddress getByAddress(String host,byte[] addr)throws UnkNownHostException

These two methods can create InetAddress instances where the host name does not exist or the host name cannot be resolved. for instance:

	public static void main(String[] args) throws Exception {
		byte[] bytes = {14,(byte) 215,(byte) 177,39};
		InetAddress inetAddress = InetAddress.getByAddress("www.doge.com",bytes);
		System.out.println(inetAddress);
	}

In fact, the domain name www.doge.com Com does not exist, but this method does not throw an exception.

If you want to query all IP addresses of a host name, you can use:

static InetAddress[] getAllByName(String host) throws UnkNownHostException

If you need to query the host name and IP address of this machine, you can use:

static InetAddress getLocalHost() throws UnkNownHostException

Note that this method will try to connect DNS to query the real host name and IP address of the local computer. If the query fails, it will return the loopback address, that is, the host name is "localhost", and the IP address is the dotted four segment address "127.0.0.1".

InetAddress cache

The overhead of DNS lookup may be quite large (it may take several seconds if the request needs to pass through multiple intermediate servers or try to resolve an unreachable host), so InetAddress will cache the DNS query results, that is, once the address of a given host is obtained, it will not be searched again, even if multiple InetAddress instances are created for the same host, The DNS query will not be performed again. Such a caching mechanism is good for performance, but it will also have a negative impact:

Therefore, Java only caches the unsuccessful DNS query results for 10 seconds, and the cache time can be controlled through the following two system variables:

Basic property acquisition method provided by InetAddress

InetAddress provides four basic attribute acquisition methods to obtain the host name and IP address represented by the current InetAddress.

public String getHostName();
public String getCanonicalHostName();
public byte[] getAddress();
public String getHostAddress();

Note that the above methods only have getter, but not setter, indicating that the setting permission of these properties is Java Net package.

The above getaddress () method also has a special judgment usage scenario, that is, if the length of its return value byte array is 4, InetAddress must be an instance of inet4address. If the length is 16, InetAddress must be an instance of inet6address. From this, we can judge whether the IP address in InetAddress is IPv4 or IPv6.

Address type judgment method provided by InetAddress

Some IP addresses and address patterns have special meanings, such as 127.0 0.1 is the local loopback address, 244.0 0.0 to 239.255 IPv4 addresses in the range of 255.255 are multicast addresses. Ten public instance methods are provided in InetAddress to determine whether the InetAddress object conforms to these address patterns:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>