The IP address and InetAddress classes in Java use the details

  • 2020-04-01 04:14:50
  • OfStack

One of the advantages of the Java language is that Java programs can access network resources. Java provides a series of classes that enable Java programs to access network resources.
TCP/IP protocol and IP address

In order to carry out network communication, both sides of the communication must abide by the communication Protocol.

TCP/IP is divided into four levels:

Network interface layer: responsible for receiving and sending physical frames; Network layer: responsible for the communication between adjacent nodes; Transport layer: responsible for the communication from the beginning to the end; Application layer: provides applications such as file transfer, E-mail, and so on.

TCP treats any network traffic as a flow of information. For example, if A long message on machine A is sent to machine B, the sending end A needs to segment the data and package and send the pieces of data separately. A packet has a header that indicates where the packet is going and where the data in the packet is in the receiving sequence. Each packet is sent from one machine to another, or from one network node to another, according to the destination provided by the IP address. At receiver B, the packets can be reassembled in the correct order.

The TCP/IP protocol is a family of protocols consisting of a set of protocols, mainly including the following more specific protocols:
Telnet (remote login) : allows a computer user to log on to another remote computer, making remote operations as if they were on a local computer.
FTP (File Transfer protocol) : allows users to copy files on remote hosts to their own computers.
SMTP (simple Mail Transfer Protocol) : used to Transfer E-mail.
NFS (Network file Server) : enables multiple computers to transparently access each other's directories.
HTTP: a hypertext transfer protocol based on the TCP/IP protocol, which is the application layer communication protocol between the WWW browser and the server. HTTP is a generic, stateless, object-oriented protocol. An HTTP session (transaction) consists of four steps: Connection, Request, Response, and Close.

The Java language lets you write low-level web applications. For example, transfer files, set up mail controllers, process network data, etc. The Internet protocol that Java language supports has FTP, Telnet, WWW, the software that supports network communication is in java.net package, for example, java.net.ftp, java.net.www and so on.

IP addresses are used to specify the network address of a computer on the Internet, representing a network address in 32-bit binary code. Addresses are divided into five categories: A, B, C, D and E. Commonly used are A, B and C:
A (1.0.0.0-126.255.255.255) : 0, 7-bit network number, and the last 24 bits main machine number;
B (128.0.0.0-191.255.255.255) : 10,14 bit network number, the last 16 bit main machine number;
C (192.0.0.0-223.255.255.255) : 110, 21-bit network number, the last 8 bits of the main machine number;
D (224.0.0.0-239.255.255.255) : 1110, 28-bit multicast group label;
E (240.0.0.0-254.255.255.255) : 1111, reserved for experimental use.

Typically, IP addresses are represented as four-bit decimal Numbers (8-bit segments). Such as:
      58.218.204.252
Or a literal domain name. Such as:
      www.jb51.net
On the Internet, Domain Name Server (DNS) performs the mapping of literal names to binary network addresses.
InetAddress class

The Java.net package has the definition of the InetAddress class. The objects of the InetAddress class are used for IP addresses and domain names. The class provides the following methods:
GetByName (String) s: gain an InetAddress class object, the object is contained in the host's IP address and domain name, the object with the information contained in the following format said it: www.sina.com.cn/202.108.37.40;
String getHostName() : gets the domain name of the InetAddress object;
String getHostAddress() : gets the IP address of the InetAddress object;
GetLocalHost () : gets an InetAddress object that contains the domain name and IP address of the local machine.

Application that explains the use of the Inetaddress class. The program demonstrates how to get the domain name and IP address of www.weixueyuan.net.


Import java.net.*;
Class Example10_1{
  Public static void main(String args[]){
    Try{ //The following code establishes the InetAddress object by domain name:
      InetAddress addr = InetAddress.getByname( " www.jb51.net " );
      String domainName = addr.getHostName();//Get host name
      String IPName = addr.getHostAddress();//Get the IP address
      System.out.println(domainName);
      System.out.println(IPName);
    }catch(UnknownHostException e){
      e.printStackTrace();
    }
  }
}

The running result is:


www.jb51.net
58.218.204.252


Related articles: