Internet query in the network programming-java

Source: Internet
Author: User

Tags: set HTTP protocol anon perceptual mechanism digital dock version


In-depth understanding of URLs, URIs and other concepts, or learning about socket-related knowledge, it is necessary to systematically understand some of the Internet-related basic knowledge.

Internet address

A device connected to the Internet (the 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 an Internet address or an IP address.

IP and domain names

If you use Java as your development language, you don't need to worry about how IP or domain names work, but we need to understand some of the basics of IP addressing. Our current common network is the IPV4 network, each computer node is a 4-byte (32bit) digital ID, the format of the digital ID is a point of four paragraphs (dotted quad, in the form of:, where each number is an unsigned byte, The value range is 0 to 255. When the data is transmitted over the network, the header of the packet should include the address of the machine to be sent to (the destination address) and the address of the machine (source) where the packet was sent.

The total number of IP addresses that can be used for the IPV4 type is probably more than 4 billion, so it is not possible for everyone on the planet to assign a unique IPV4 IP address, so IPv6 is born, and the network is now IPv4 to IPV6 (although the process is relatively slow and many factors). The IP address in the IPV6 network uses a digital ID of 16 bytes (128bit). The representation of an IPV6 address is typically 8 regions separated by a colon, each of which is 4 hexadecimal digits, for example: fedc:ba98:7654:3210:fedc:ba98:7654:3210 is a legitimate IPV6 address. In a mixed network of IPV4 and IPv6, the last four bytes of the IPV6 address are sometimes represented as a four-segment address with a point of IPv4 address. IPV6 addresses are supported only in Jdk1.4 and later versions, in other words, Jdk1.3 or previous versions can only use IPV4 addresses.

While computers can easily handle numbers, the memory of the human brain is not sensitive to numbers, so the domain Name system, known as DNS, is used to convert a host name (such as that is easily remembered by the human brain to a digital internet address (e.g. This does not expand the specific content of the DNS, as a developer, we can simply understand that it is a huge distributed database, used to map the hostname (domain name) and IP address, drawing a diagram is as follows:


Because each computer does not do one thing, the equivalent of every business logic that the computer does need to be logically isolated, such as the processing of FTP requests and the separation of the processing of e-mail, FTP request processing and the web business processing separation, so the processing of each business logic needs to use a logically separated identity, This identity is the port. In general, each computer has thousands of logical ports (specifically, each Transport layer protocol has 65,535 ports, the Windows system, the 1~1023 port is the system port, the user cannot modify, the 1024~65534 port is the system for the user reserved port, While Port No. 65535 is the system reserved port, each port can be assigned to a specific service. For example, the Web's underlying protocol HTTP protocol traffic typically uses port 80, and using the browser URL to access the server's 80 port can omit the port number from the URL.

Java's abstraction of the network inetaddress

The word inetaddress is an abbreviated merge of the Internet address, representing the Internet addresses. The class is a highly abstract representation of Java for IP addresses (including IPV4 and IPV6 addresses). Most network programming related classes will use inetaddress, such as sockets, ServerSocket, etc., inetaddress the two most core properties are hostname (host) and IP address, corresponding attributes hostname and address.

Creating an InetAddress instance

Creating an InetAddress instance relies heavily on its factory method (in fact the InetAddress constructor is a private package, i.e. it cannot be created with the New keyword), and a common static factory method is:

static InetAddress getByName(String host) throws UnknownHostException

Where the parameters can be a host name (domain name) or a four-segment address, the former equivalent to find a host name can connect IP address, which is equivalent to the IP address to reverse the hostname, it is worth noting that the use of this method will establish a connection with the local DNS server for hostname or digital address lookup, If the DNS server cannot find the host or address, it throws an Unknownhostexception exception.

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

InetAddress overwrite the toString method and return the result is the hostname/address format, one of the possible results of the Main method above is:

Sometimes, when we know the digital IP address, we can create a inetaddress instance directly from the digital address so that you don't have to use getByName(String host) method and DNS interaction.

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

Both methods can create inetaddress instances where the hostname does not exist or the host name cannot be resolved. As an example:

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

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

If you want to query all IP addresses for 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 local computer's real hostname and IP address, if the query fails, it will return the loopback address, that is, the hostname is "localhost", the IP address is the point of four addresses "".

InetAddress Cache

The cost of DNS lookups can be quite large (if the request needs to go through multiple intermediary servers or attempt to resolve an unreachable host, which can take a few seconds), so inetaddress caches the DNS query results, that is, once the address of a given host is obtained, it will not be looked up again. Even if multiple inetaddress instances are created for the same host, no DNS queries are made again. This caching mechanism is good for performance, but it can also have a negative impact:

    • The IP addresses of the hosts connected during the program run can vary greatly, and the cached IP may not be available.
    • The initial attempt to parse a host is unsuccessful, but subsequent attempts to parse it will succeed, but the record for the first parse failure is cached.
    • The information sent by the remote DNS server is still being transmitted, the first attempt times out, and the next request succeeds.

As a result, Java caches only 10 seconds for unsuccessful DNS query results, and can control the cache time by using the following two system variables:

    • Networkaddress.cache.ttl: The cache time (in seconds) of a successful DNS query result, 1 means no time-out.
    • Networkaddress.cache.negative.ttl: The cache time (in seconds) of a successful DNS query result, 1 means no time-out.
Basic property acquisition methods provided by InetAddress

InetAddress provides four basic property acquisition methods for obtaining the host name and IP address that the current inetaddress represents.

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

Note that the above methods are only getter, there is no setter method, stating that the set permissions for these properties are the class library in the package.

    • GetHostName: Returns the host name of the current InetAddress instance, and returns a four-digit IP address if the corresponding machine does not have a hostname or the security Manager prevents the host name from being determined.
    • Getcanonicalhostname:getcanonicalhostname and GetHostName, but the GetHostName method is only in the case of the host name is not known to connect DNS for query, The Getcanonicalhostname method always connects the DNS query host name and replaces the cached value, so this method call can be time-consuming.
    • GetAddress: Returns a byte array of the digital IP address of the current InetAddress instance, note that because there is no unsigned byte in Java, a negative byte value of +256 becomes an unsigned byte value.
    • Gethostaddress: Actually returns a byte array in the GetAddress method into a four-segment representation of the IP address point, which is the IP address string.

The above getAddress() method also has a special judgment of the use of the scene, that is, its return value byte array length if it is 4, then inetaddress must be an instance of inet4address, if the length is 16, So then inetaddress must be inet6address instance, thus can judge inetaddress IP address is IPv4 or IPv6.

InetAddress provides the method of determining the address type

Some IP address and address patterns have special meanings, such as is the local loopback address, and the IPV4 address in the range to is the multicast address. InetAddress provides 10 public instance methods to determine if the InetAddress object conforms to these address patterns:

    • 1, boolean isAnyLocalAddress() : If the address is a wildcard address returns true, the so-called wildcard address is can match any address in the local system, the IPV4 address in the is the IPV6, the address is 0:0:0:0:0:0:0:0 (::).
    • 2 boolean isLoopbackAddress() : If the address is loopback address returns True, the loopback address in IPV4 is, and the loopback address in IPv6 is 0:0:0:0:0:0:0:1 (:: 1).
    • 3 boolean isLinkLocalAddress() : Returns True if the address is a IPV6 local link address.
    • 4 boolean isSiteLocalAddress() : Returns True if the address is a IPV6 local Web site address.
    • 5 boolean isMulticastAddress() : Returns True if the address is a multicast address.
    • 6 boolean isMCGlobal() : Returns True if the address is a global multicast address.
    • 7 boolean isMCOrgLocal() : Returns True if the address is an organization-wide multicast address.
    • 8 boolean isMCSiteLocal() : Returns True if the address is a site-wide multicast address.
    • 9 boolean isMCLinkLocal() : Returns True if the address is a subnet range multicast address.
    • 10 boolean isMCNodeLocal() : Returns True if the address is a local interface multicast address.

In fact, we seldom use these 10 methods.

InetAddress test for accessibility

InetAddress provides two isReaachable() methods for testing accessibility. is to test whether a particular node is available to the current host (whether they can establish a network connection). Because network connections can be blocked for a variety of reasons, some of the reasons are listed below:

    • Firewall blocking.
    • Proxy server interception.
    • A router that is not functioning properly.
    • Disconnect the network cable.
    • The remote computer you are trying to connect to is not powered on.
public boolean isReachable(int timeout) throws IOExceptionpublic boolean isReachable(NetworkInterface netif, int ttl, int timeout) throws IOException

Both methods try to use traceroute to see if the specified address is up to. The Traceroute program uses a TTL field (typically 64) in the ICMP packet and IP header. The purpose of the TTL field is to prevent datagrams from endlessly flowing through the network during routing (when a route fails, it may be in two route loops). You can understand that the TTL field is used to control the maximum number of network hops before a connection is discarded. The first method has isReachable(int timeout) only one parameter that controls the number of timeout milliseconds to detect accessibility, and the second method can control the specified local network interface, TTL parameters, and timeout times for accessibility testing.

    public static void main(String[] args) throws Exception{        InetAddress inetAddress = InetAddress.getByName("");        System.out.println(inetAddress.isReachable(2000));    }
Compare the different inetaddress

InetAddress in the equals hashCode method, compared to the time, in fact, compared to the Address property, which is the IP address, in other words, as long as the IP address of two inetaddress consistent, the two InetAddress objects are equal, The hostname of the two InetAddress objects is not required to be identical. As an example:

    public static void main(String[] args) throws Exception {        while (true){            Thread.sleep(500);            InetAddress inetAddress = InetAddress.getByName("");            InetAddress other = InetAddress.getByName("");            System.out.println(inetAddress.equals(other));        }    }

After the main method above executes, it basically prints true, depending on the processing of the DNS.

Inet4address and Inet6address

Inet4address and inet6address two classes inherit from InetAddress, which are highly abstract representations of the IP addresses of IPV4 and IPV6, respectively. However, in general, developers make it unnecessary to use or connect IP addresses that are IPv4 and IPV6 IP addresses, because usually we are accessed through the host name.

Local network interface

NetworkInterface is a local network interface, which can actually represent a physical interface, such as an Ethernet card, which can also represent a virtual interface that is bound to the same physical hardware as the other IP addresses of the machine, most commonly the current virtualization container, such as the NIC provided by Docker. Some of the methods provided by the NetworkInterface class can enumerate all local addresses, create inetaddress objects from these local addresses, and create these inetaddress objects that can be used on the client socket or server socket.

How to create a NetworkInterface

getByName(String name)The NetworkInterface method can specify the network interface name to get the corresponding network interface instance, or null if there is no such name for the network interface. Network interface name format and platform-related, such as typical UNIX system, Ethernet interface name in the form of eth0, eth1, etc., in the Windows system name similar to "CE31", "ELX100" and other strings, "Lo" is generally the local loopback address of the network interface name.

In addition, you can return a specified IP-bound network interface via InetAddress (or the network interface that is returned to handle the specified IP address), and if the local host does not have a network interface and an incoming IP address binding, returns NULL, and throws a socketexception if an error occurs.

Finally, you can use the following method to enumerate all network interfaces on the local host.

static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException

The author uses the WINDOWS10 system, trying to enumerate all the network interfaces:

    public static void main(String[] args) throws Exception{        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();        while (networkInterfaces.hasMoreElements()){            NetworkInterface networkInterface = networkInterfaces.nextElement();            System.out.println(networkInterface);        }    }

The results of the implementation section are as follows:

name:lo (Software Loopback Interface 1)name:ppp0 (WAN Miniport (PPPOE))name:net0 (Microsoft ISATAP Adapter #2)name:net1 (Microsoft ISATAP Adapter)name:net2 (WAN Miniport (L2TP))name:net3 (WAN Miniport (IKEv2))...
NetworkInterface provides a property acquisition method

NetworkInterface provides an instance method public Enumeration<InetAddress> getInetAddresses() for getting all IP addresses that are bound to a network interface, although this is not common, but it does exist.

The instance method public String getName() returns the object name of the NetworkInterface instance, such as Eth0, Lo, and so on.

The instance method public String getDsiplayName() is also the object name that returns the NetworkInterface instance, but the representation is more friendly, such as "Ethernet Card 0" (eth0), but in the UNIX system and the getName() same.


The path to learning URI (URL), TCP protocol, HTTP protocol, and socket (socket) can be paved only by understanding some of the basic concepts in network programming.

(End of this article c-2-d)

Internet query in the network programming-java

Related Article

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.

Tags Index: