This article is the first Java network programming, introduces the basic knowledge of the network.

1. The network

A network is a collection of computers and other devices that can send and receive data to each other almost in real time.

  • Node: Each machine in the network is called a node
  • Host: A computer node with complete empty energy
  • Address: A sequence of bytes that uniquely identifies a node
  • Protocol: A set of explicit rules that define how computers communicate, including address formats, how data is subcontracted, and so on.

2. Network layer

To hide complexity from application developers and end users, different aspects of network communication are decomposed into layers. Each layer is represented as a different level of abstraction between the physical hardware and the transmitted information.

The traditional OSI model divides network transport into seven independent layers, as shown in the following table

The hierarchy instructions
The application layer Services provided by the application
The presentation layer Format the data to provide a common interface to the application
The session layer Establish an end connection between two nodes
The transport layer TCP and UDP are transport layer protocols for connection-oriented or connectionless conventional data delivery
The network layer Through addressing to establish a connection between two nodes, IP protocol is a network layer protocol
The link layer Frame the data, add checksum, and handle flow control
The physical layer Transmission of raw bitstream

Application layer, presentation layer and session layer are generally implemented by program developers. The transport layer, network layer, data link layer, and physical layer are provided by the operating system.

The following uses sending “Hello” from a client to a server as an example to illustrate the network transmission process.

2.1 the application layer

The functionality of the application layer is the functionality provided by the application. In the sending “hello” example, the user converts the string “hello” into a secondary stream to the transport layer.

2.2 the transport layer

TCP is used as an example to illustrate the transport layer. TCP provides reliable data transmission in the IP environment. It implements data transmission, reliability verification, and flow control.

This is to add a flag to the data, “TCP header “.

2.3 the network layer

The IP protocol is used as an example. The IP protocol is used to connect multiple packet switching networks. It transmits packets between source and destination addresses. It also provides the ability to reassemble data to accommodate different packet size requirements of different networks.

Similarly, IP protocols add information such as the destination address (IP address and port) to the data, and split the data if necessary.

2.4 Data link Layer

In order to retransmit only the limited data with errors if errors occur in transmission, the data link layer combines bitstreams into frames and transmits them in frames.

2.5 the physical

Physical layer transmission refers to the process of data transmission through physical media, such as cables and optical fibers. The data is sent through the physical medium to the destination, which then performs the reverse process to get the string “hello”.

3. Agreement

3.1 IP

3.1.1 IP address

Computers on a network are identified by IP addresses, and applications communicate with each other through communication ports.

3.1.2 port

A port is a paraphrases of the English port, which is an outlet for communication between a device and the outside world. A total of 65536 ports can be allocated to each computer from 0 to 65535. Ports 0 to 1023 are known as port numbers and are assigned to fixed services, such as WWW service on port 80 and FTP service on port 21.

3.1.3 domain name

Also known as a domain, it is the name of a computer or computer group on the Internet composed of a series of dotted names. It is used to identify a computer during data transmission (sometimes also refers to its geographical location).

3.2 the TCP protocol

TCP is a connection-oriented, reliable, byte stream based transport layer communication protocol, while ITS counterpart, UDP, is connectionless and unreliable.

3.2.1 Establishing a TCP Connection

Three handshakes to establish a connection, the process is as follows:

  • The client sends SYN seq=x to the server
  • The server receives data and sends SYN seq=y,ACK=x+1 to the server
  • ACK=y+1 from the client to the server

3.2.2 TCP Data Transmission

After sending a piece of data, there is no guarantee that the data will be received by the recipient. The sender then waits for a response from the receiver and resends the data if it takes too long to receive a response.

3.2.3 TCP Connection Termination

TCP makes sure both ends are disconnected by waving four times.

First wave: Host 1 (either client or server) sends a FIN signal to host 2. At this point, host 1 enters the FIN_WAIT_1 state. It has no data to send and only needs to wait for a response from Host 2.

Second wave: Host 2 receives a FIN signal from host 1 and responds with an ACK. Host 1 that receives an ACK enters the FIN_WAIT_2 state.

Third wave: After host 2 sends all data, host 2 sends a FIN signal to host 1 to close the connection.

Fourth wave: Host 1 responds to host 2 with an ACK after receiving the FIN signal from host 2. Host 1 then enters the TIME_WAIT state (waits for a period of time to process retransmissions from host 2). Host 2 closes the connection after receiving the response from host 1. At this point, TCP’s four waves are complete, and both hosts 1 and 2 close the connection.

The resources

  1. Java Web Programming
  2. Unity 3D Online Game Combat