Java Network Programming: Finding Internet addresses

Source: Internet
Author: User

Java Network Programming: Finding Internet addresses
1. Overview computers connected to the Internet are identified by a unique number called an Internet address or IP address. Since IP addresses are hard to remember, people have designed a Domain Name System (DNS), which can link host names that people can remember with IP addresses that computers can remember. Generally, a host maps an IP address. Sometimes a host name maps multiple IP addresses. In this case, the DNS is responsible for randomly selecting a machine to respond to requests. For example, a website with high business traffic distributes the load to multiple systems. Go Back to Top 2. What is an IP address? An IP address is a 32-bit or 128-bit unsigned number used by an IP address. It is a low-level protocol. Both UDP and TCP are built on it. 1. Address type unicast: the identifier of a single interface. The packets sent to the unicast address are delivered to the interface marked by this address. Multicast: the identifier of a group of interfaces (usually different nodes). packets sent to multicast addresses are delivered to all interfaces marked by this address. 2. IP range link local: The address is designed to address a single link to solve problems such as automatic Address Configuration, neighbor discovery, or no router. Local Site: The address is designed to address the site when a global prefix is not required. Global Address: It is unique on the Internet. 3. IP address text representation the text representation of an IP address is specific to the address series. An IPV4 address is generally written into four unsigned bytes, each of which ranges from 0 to 255. This method is also called the four-segment format. Eg: 192.168.1.119. Return to top 3. The InetAddress class indicates the Internet Protocol (IP) address, which is a high-level representation of the IP address in Java. Used for most other network types. An instance of the InetAddress class contains an IP address and may also contain the corresponding host name (depending on whether it is constructed using the host name or whether reverse host name resolution has been performed ). 1. There is no public constructor for constructing the InetAddress object InetAddress class. You can use its static method to return the appropriately initialized InetAddress object. Static InetAddress [] getAllByName (String host) Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. static InetAddress getByName (String host) Determines the IP address of a host, given the host's name. static InetAddress getLocalHost () Returns the address of the local host. ps: You only need to point out that these methods not only use their parameters to set internal fields, but also need to perform network connections to obtain all the required information. Other methods of this class mainly use the information provided by the above method to work. Because DNS search costs are relatively high, you can use networkaddress. cache. ttl to specify the number of seconds that a successful DNS search will be retained in the Java cache. In addition to the local cache in the InetAddress class, DNS servers in the local host, Local Domain Name Server, and other places in the Internet also cache various search results. 2. Class Method boolean equals (Object obj) Compares this object against the specified object. byte [] getAddress () Returns the raw IP address of this InetAddress object. static InetAddress [] getAllByName (String host) Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. static InetAddress getByAddress (byte [] addr) Returns an InetAddress object given the raw IP address. static InetAddress getByAddress (String host, byte [] addr) Creates an InetAddress based on the provided host name and IP address. static InetAddress getByName (String host) Determines the IP address of a host, given the host's name. string getCanonicalHostName () Gets the fully qualified domain name for this IP address. string getHostAddress () Returns the IP address string in textual presentation. string getHostName () Gets the host name for this IP address. static InetAddress getLocalHost () Returns the address of the local host. static InetAddress getLoopbackAddress () Returns the loopback address.int hashCode () Returns a hashcode for this IP address. 3. Sample InetAddress code

Public class Demo1 {public static void main (String [] args) {InetAddress ina; try {ina = InetAddress. getLocalHost (); System. out. println (ina); System. out. println (ina. getAddress (); // return the original IP address of the InetAddress object System. out. println (ina. getHostAddress (); // returns an IP address string (in text format ). System. out. println (ina. getHostName (); // obtain the Host Name System of this IP address. out. println (ina. getLocalHost (); // returns the local host} catch (UnknownHostException e) {// catch Block e automatically generated by TODO. printStackTrace ();} InetAddress ina1; try {ina1 = InetAddress. getByName ("192.168.1.119"); System. out. println (ina1);} catch (UnknownHostException e) {// catch Block e automatically generated by TODO. printStackTrace ();} InetAddress [] ina2; try {ina2 = InetAddress. getAllByName ("www.microsoft.com"); for (int I = 0; I <ina2.length; I ++) System. out. println (ina2 [I]);} catch (UnknownHostException e) {// catch Block e automatically generated by TODO. printStackTrace ();}}}

 

 

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.