One of the advantages of the Java language is that Java programs can access network resources. Java provides a series of classes to support Java programs in accessing network resources.
TCP/IP protocol and IP address
In order to communicate on the Internet, both communicating parties must comply with the communication protocol. The most widely used protocol at present is the TCP/IP protocol, which is a public protocol followed by all parties in the Internet. TCP (Transport Control Protocol) is a transmission control protocol, and IP ( Internet Protocol) is an Internet protocol, and TCP/IP represents these two protocols.
TCP/IP is divided into four levels:
The TCP protocol treats any network information transmission as an information flow. For example, if a long message is sent from machine A to machine B, sender A needs to fragment the data and package and send each piece of data separately. The data packet has a header that indicates where the data packet is sent and the position of the data in the receiving sequence. Each packet is transmitted from one machine to another machine or from one network node to another network node according to the destination provided by the IP address. At the receiving end B, these data packets can be reassembled in the correct order.
The TCP/IP protocol is a protocol suite, consisting of a set of protocols, mainly including the following more specific protocols:
Telnet (remote login): allows a computer user to log in to another remote computer, making remote operations as if they were operating on the local computer.
FTP (File Transfer protocol): allows users to copy files on a remote host to their own computer.
SMTP (simple Mail Transfer Protocol): used to transfer email.
NFS (Network file Server): enables multiple computers to transparently access each other's directories.
HTTP: A hypertext transfer protocol, which is based on the TCP/IP protocol and is the application layer communication protocol between the WWW browser and the server. HTTP is a general-purpose, stateless, object-oriented protocol. HTTP session (transaction) consists of four steps: Connection, Request, Response and Close.
The Java language can write low-level network applications. For example, transfer files, build mail controllers, process network data, etc. The Internet protocols supported by the Java language include ftp, telnet, www, etc. The software that supports network communication is in the java.net package, for example, java.net.ftp, java.net.www, etc.
The IP address is used to indicate the address of a computer on the Internet in the network, using a 32-bit binary code to represent a network address. Addresses are divided into five categories: A, B, C, D, and E. The most commonly used categories are A, B, and C:
A (1.0.0.0-126.255.255.255): 0,7 digits network number, the last 24 digits are the host number;
B (128.0.0.0-191.255.255.255): 10, 14-digit network number, the last 16 digits are the host number;
C (192.0.0.0-223.255.255.255): 110, 21-digit network number, the last 8 digits are the host number;
D (224.0.0.0-239.255.255.255): 1110, 28-bit multicast group number;
E (240.0.0.0-254.255.255.255): 1111, reserved for test use.
Usually, an IP address is represented by a four-segment decimal number (one segment of 8 digits). For example:
58.218.204.252
Or represented by a text domain name. For example:
www.VeVB.COm
On the Internet, Domain Name Server (DNS) performs the mapping of literal names to binary network addresses.
InetAddress class
There is a definition of the InetAddress class in the Java.net package. The objects of the InetAddress class are used for IP addresses and domain names. This class provides the following methods:
getByName(String s): Obtain an object of the InetAddress class, which contains the IP address and domain name of the host. The object represents the information it contains in the following format: www.sina.com.cn/202.108.37.40;
String getHostName(): Get the domain name of the InetAddress object;
String getHostAddress(): Get the IP address of the InetAddress object;
getLocalHost(): Obtain an InetAddress object, which contains the domain name and IP address of the local machine.
[Example] An application that explains the usage of the Inetaddress class. The program demonstrates how to obtain 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 creates an InetAddress object through the domain name: InetAddress addr = InetAddress.getByname("www.VeVB.COm"); String domainName = addr.getHostName();//Get the 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.VeVB.COm58.218.204.252