Java know how much (103) network programming IP address and InetAddress class

Source: Internet
Author: User
Tags domain name server file transfer protocol

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.

The TCP/IP protocol and address

In order to communicate with the network, both parties must abide by the communication protocol. The most widely used is the TCP/IP protocol, which is the common protocol that is followed by all parties on the Internet. TCP (Transport Control Protocol) is a transmission protocol, IP ( Internet Protocol) is an Internet 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 start-to-end communication;
    • Application tier: Provides applications such as file transfer, e-mail, and more.


The TCP protocol treats any network information as a stream. For example, a long message on machine A is sent to Machine B, and send side 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 in the killer is located in the receive sequence. Each package is routed from one machine to another at the destination provided by the IP address, or from one network node to another network node. On the receiving side B, these packets can be reassembled in the correct order.

The TCP/IP protocol is a protocol family consisting of a set of protocols that mainly contain the following more specific protocols:

    • Telnet: Allows one computer user to log on to another remote computer so that the remote operation behaves as if it were on the local computer.
    • FTP (File Transfer protocol): Allows a user to copy files on a remote host to their own computer.
    • SMTP (Simple Mail Transfer Protocol): Used to transfer e-mail messages.
    • 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, is the application Layer communication protocol between WWW browser and server. HTTP is a generic, stateless, object-oriented protocol. An HTTP session (transaction) consists of four steps: Connect (Connection), request, Answer (Response), and close.


The Java language can write low-level Web applications. For example, transferring files, establishing a mail controller, processing network data, and so on. The Java language supports Internet protocols such as FTP, telnet, www, etc., and the software that supports network communication is in the java.net package, for example, Java.net.ftp, java.net.www, and so on.

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 sub-Category A, B, C, D, E Five, commonly used are a, B, c three categories:

    • A (1.0.0.0-126.255.255.255): 0, 7-bit network number, the latter 24-bit is the host number;
    • B (128.0.0.0-191.255.255.255): 10, 14-bit network number, the latter 16-bit is the host number;
    • C (192.0.0.0-223.255.255.255): 110, 21-bit network number, the latter 8-bit is the host number;
    • D (224.0.0.0-239.255.255.255): 1110, 28-bit multipoint broadcast Group label;
    • E (240.0.0.0-254.255.255.255): 1111, reserved for trial use.


Typically, the IP address is represented by a four-segment decimal number (8-bit paragraph). For example:
116.255.226.187
Or with a text domain name. For example:
Www.weixueyuan.net
On the Internet, the name server (domain name Server,dns) performs a mapping of a text name to a binary network address.

InetAddress class

The InetAddress class is defined in the java.net package, and the object for the InetAddress class is used for the IP address and domain name, which provides the following methods:

    • Getbyname (String s): Gets an object of the InetAddress class that contains the IP address and domain name of the host that represents the information it contains in the following format: 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 a InetAddress object that contains the domain name and IP address of the local machine.


"Example 13-1" describes an application that uses the InetAddress class. The program demonstrates how to get the domain name and IP address of www.weixueyuan.net.

1Import java.net.*;2 Class example10_1{3PublicStatic voidMain (String args[]) {4try{//The following code establishes the InetAddress object through the domain name:5InetAddress addr =inetaddress.getbyname ("www.cnblogs.com");6String domainName = Addr.gethostname ();//Get host name7String ipname = addr.gethostaddress ();//get an IP address8 System.out.println (domainName);9 System.out.println (ipname);Ten}Catch(unknownhostexception e) { One e.printstacktrace (); A         } -     } -}

The result of the operation is:

Www.cnblogs.com
42.121.252.58

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawingJava know how much (97) Drawing Mode OverviewHow much Java knows (98) the drawing method of the Graphics classJava know how much (graphics2d) the drawing method of the classJava know how much (100) Image processing BasicsJava know how much (101) image buffering TechnologyJava know how much (102) Multimedia Foundation

Java know how much (103) network programming IP address and InetAddress class

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.