One of the advantages of the Java language is that Java programs can access network resources. Java provides a series of classes that support Java programs to access network resources.
TCP/IP protocol and address
For network communication, both parties must comply with the communication protocol. The most widely used is the TCP/IP protocol, which is the common protocol followed by all parties on the Internet. TCP (Transport Control Protocol) is a Transmission Control protocol, IP ( Internet Protocol) is an internetwork protocol that TCP/IP represents for both protocols.
TCP/IP is divided into four levels:
- Network Interface layer: responsible for receiving and sending physical frames;
- Network layer: Responsible for the communication between neighboring nodes;
- Transport layer: Responsible for the beginning to the end of the communication;
- Application layer: Provides applications such as file transfer, e-mail, and so on.
The TCP protocol treats any network information as a stream of traffic. For example, a long message on machine A is sent to machine B, and sending end a needs to fragment the data and send a piece of data separately. A packet has a header that indicates where the packet is sent, and where the data is located in the receiving sequence. Each packet is routed from one machine to another in accordance with the destination provided by the IP address, or from one network node to another network node. At receiver B, these packets can be reassembled in the correct order.
The TCP/IP protocol is a family of protocols consisting of a set of protocols that consist mainly of the following more specific protocols:
Telnet (remote login): Allows one computer user to log on to another remote computer so that remote Operations act as if they were on the local computer.
FTP (File Transfer protocol): Allows users to copy files on a remote host to their computer.
SMTP (Simple Mail Transfer Protocol): Used to transfer e-mail messages.
NFS (network files server, network file servers): Allows multiple computers to transparently access each other's directories.
HTTP: A Hypertext Transfer Protocol that is based on the TCP/IP protocol and 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: a connection (Connection), a request (requests), a reply (Response), and a shutdown (close).
The Java language can write low-level network applications. For example, transfer files, establish a mail controller, handle network data, and so on. The Java language-supported Internet Protocol has FTP, telnet, www, and so on, software that supports network communication is in the java.net package, for example, JAVA.NET.FTP, java.net.www, etc.
An IP address is used to indicate the address of a computer on the Internet in a network, with 32-bit binary code representing a network address. Address points A, B, C, D, e Five categories, commonly used in a, B, c three categories:
A (1.0.0.0-126.255.255.255): 0, 7-bit network number, the latter 24 digits for the host number;
B (128.0.0.0-191.255.255.255): 10, 14-bit network number, the latter 16 digits for the host number;
C (192.0.0.0-223.255.255.255): 110, 21-bit network number, the latter 8 digits for the host number;
D (224.0.0.0-239.255.255.255): 1110, 28-bit multicast group marking;
E (240.0.0.0-254.255.255.255): 1111, reserved for trial use.
Typically, an IP address is represented by a four-segment decimal number (8-bit). For example:
58.218.204.252
Or in a literal domain name. For example:
Www.jb51.net
On the Internet, the name server (domain name Server,dns) performs a mapping of literal names to binary network addresses.
InetAddress class
The java.net package contains the definition of the InetAddress class, where objects for the InetAddress class are used for IP addresses and domain names, which provide the following methods:
Getbyname (String s): Gets an object of the InetAddress class that contains the IP address and domain name of the host, which represents the information it contains: 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.
Example describes the application of the use of the InetAddress class. The program demonstrates how to obtain the Www.weixueyuan.net domain name and IP address.
Import java.net.*;
Class example10_1{public
static void Main (String args[]) {
try{//The following code establishes the InetAddress object through the domain name:
inetaddress Addr = Inetaddress.getbyname ("www.jb51.net");
string domainname = Addr.gethostname ()//Get host name
String ipname = addr.gethostaddress ()//Get IP address
SYSTEM.OUT.PRINTLN (domainname);
System.out.println (Ipname);
} catch (Unknownhostexception e) {
e.printstacktrace ();}}}
The results of the operation are:
Www.jb51.net
58.218.204.252