Three elements of network communication in Java 25-2 Network programming

Source: Internet
Author: User

Three elements of network communication

IP Address:

InetAddress the identity of the device in the network, it is not easy to remember, the host name can be used (identification number of the computer)

Port number:

The logical address that identifies the process, the identity of the different processes (the identification number of the software that is running)

Transport protocol:

Rules of Communication Common protocol: TCP,UDP

Example:  

What elements do we need to communicate in peacetime?

For example, I want to talk to you.

First condition:

I need to find you first (IP)

Second condition:

You have to have the place to receive the data. Ear (port)

A third condition:

I talk to you, you can receive, I accept the way, I speak English you understand, say Korean you understand, do not understand, so I still speak Chinese. Agreement

IP Address

To allow computers in your network to communicate with each other, you must specify an identification number for each computer, which specifies the computer to which you want to accept data and the computer that identifies the send. In the TCP/IP protocol, this identification number is the IP address.

So, what if we get and manipulate IP addresses? To facilitate our access to and operation of IP addresses, Java provides a class inetaddress for us to use.

A: The so-called IP address is a 32bit address assigned to each host connected to the Internet.

In accordance with the TCP/IP rules, the IPs are represented by binary, each IP address is 32bit long and bits are converted to bytes, which is 4 bytes. For example, an IP address that takes the form of a binary

Is "00001010000000000000000000000001", so long address, people are too laborious to deal with.

In order to facilitate people's use, IP addresses are often written in decimal form, using symbols in the middle "." Separate the different bytes.

As a result, the above IP address can be represented as "10.0.0.1". This notation of IP addresses is called "Dotted decimal notation", which is obviously much easier to remember than 1 and 0.

B:ip the composition of the address

IP address = network number + host address

Class A IP address:

The first number is the network number, the remaining three segment number is the local computer's number

Class B IP Address:

The first two paragraphs are the network number, and the remaining two segments are the local computer's number.

Class C IP Address:

The first three paragraphs are the network number, the remaining number is the local computer's number

Special Address:

127.0.0.1 loopback address that can be used to test the local network for problems. Ping 127.0.0.1

DOS command ipconfig: view native IP address

xxx.xxx.xxx.0 Network Address
xxx.xxx.xxx.255 Broadcast Address

Class A 1.0.0.1---127.255.255.254 (1) 10.x.x.x is a private address (the private address is not used on the Internet, but is used in the local Area network address) (2) 127.x.x.x is a reserved address, used for cyclic testing.

Class B 128.0.0.1---191.255.255.254172.16.0.0---172.31.255.255 is a private address. 169.254.X.X is a reserved address.

Class C 192.0.0.1---223.255.255.254192.168.x.x is a private address

Class D 224.0.0.1---239.255.255.254

Class E 240.0.0.1---247.255.255.254

Class InetAddress:

This class does not have a constructor, so how do you make the functionality available to the class?

If a class does not have a constructor method:
A: The members are all static (Math,arrays,collections)
B: Single case design mode (Runtime)
C: There is a static method in the class that returns the object of the Class (InetAddress)
    

1 class Demo {2private  demo () {}34      Public Static Demo getXxx () {5     return New Demo (); 6     }7 }          

Features to Master

Get the IP of any host (you have to know the name of that host): Getbyname

Host Name: GetHostName

Host IP Address: gethostaddress

Code:

1      Public Static voidMain (string[] args)throwsunknownhostexception {2         //InetAddress class3         4         //get the IP of any host (you have to know the name of that host): Getbyname5InetAddress id = inetaddress.getbyname ("DESKTOP-9HCB2BG");6 System.out.println (ID);7         8         //Host Name: GetHostName9String name =id.gethostname ();Ten System.out.println (name); One          A         //Host IP address: gethostaddress -String myID =id.gethostaddress (); - System.out.println (myID); the}

Port number

Physical Port: Nic Port

Logical port: We're talking about logical ports.

A: Each network program will have at least one logical port

B: The logical address used to identify the process, the identity of the different processes

C: Valid port: 0~65535, where the 0~1024 system uses or retains the port.

PS: You can view the port number by using the Task Manager's details

Protocol UDP and TCP

UDP (commonly known as: Unreliable protocol)

    The data source and destination are encapsulated into a datagram packet, no connection is required, the size of each datagram packet is limited to 64k, because there is no connection, it is unreliable, it is not necessary to establish a connection, fast
TCP (commonly known as: three-time Handshake protocol)

    establish a connection to form a channel for transmitting data , make a large data transfer in connection, complete the connection through three handshake, be a reliable protocol; must establish a connection, the efficiency will be slightly lower

Summarize:
Features of UDP:

For no connection.

Not reliable.

Fast speed.

The data packet is transmitted with a maximum packet of 64k.

Example:

Chat messages, online videos, video conferencing, texting, post office parcels.

Features of TCP:

Connection oriented.

Safe and reliable

The efficiency is slightly lower.

A three-time handshake ensures the connection is established.

Example:

Download, call, QQ chat (you are online, online, just respond and start chatting)

Three elements of network communication in Java 25-2 Network programming

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.