Android advanced network protocol and network knowledge

OSI seven-layer network model

In order to enable computers produced by different manufacturers to communicate with each other and establish a wider range of computer networks, the International Organization for Standardization (ISO) proposed the "Open System Interconnection Reference Model" in 1984, namely the OSI/RM model (Open System Interconnection/Reference Model). ).

The OSI model divides the communication protocol of the computer network architecture into seven layers, each layer is built on its lower layer and provides certain services to its upper layer. The upper layer just calls the services provided by the lower layer, and does not care about the specific implementation details.

The seven-layer model from bottom to top is: physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer. The lower four layers complete data transmission, and the upper three layers face users.

OSI model

Some models are divided into 5 layers;

 

network layering

  • 1: Physical layer
    This layer is responsible for the transmission of bit streams between nodes, that is, responsible for physical transmission. The protocol of this layer is related to both the link and the transmission medium. Generally speaking, it is the physical means to link computers.

  • 2. The data link layer
    provides reliable data transmission through physical network links. The main function is how to reliably transmit data on unreliable physical lines. If the receiving point detects errors in the transmitted data when transmitting data , it is necessary to notify the sender to resend the frame.

  • 3. Network layer
    This layer decides how to route the data from the sender to the receiver. The network layer decides from one network node A to another network by considering the priority of sending, the degree of network congestion, the quality of service and the cost of the optional routing The best path for node B.

  • 4. Transport layer
    This layer provides end-to-end communication for applications on two hosts. In contrast, the function of the network layer is to establish host-to-host communication. The transport layer has two transport protocols: TCP (Transmission Control Protocol) Transmission Control Protocol) and UDP (User Datagram Protocol), TCP is a reliable connection-oriented protocol, and UDP is an unreliable or connectionless protocol.

  • 5. After the application layer
    application receives the data of the transport layer, the next step is to interpret it. The interpretation must specify the format in advance, and the application layer specifies the data format of the application. Its main protocols are HTTP, FTP, Telnet , SMTP , POP3, etc.

TCP transport

TCP transport

The TCP protocol is considered a stable protocol because it has the following characteristics:

  • Connection-oriented, "three-way handshake"
  • two-way communication
  • Ensure that data is sent in order and arrives in order
  • timeout retransmission

TCP's three-way handshake and four-way wave

TCP three-way handshake

Why three handshakes?

The essence of this problem is that the channel is unreliable, but the two-way communication needs to agree on a certain problem. And to solve this problem, no matter what information you include in the message, the three-way communication is the theoretical minimum. So the three-way handshake is not The requirement of TCP itself is caused by the requirement of "reliably transmitting information on an unreliable channel". Please note the essential requirement here, the channel is unreliable, and the data transmission must be reliable. If it is met three times, then you will Whether you want to continue the handshake or send data, it has nothing to do with the need for reliable information transmission. Therefore, if the channel is reliable, that is, whenever a message is sent, the other party will definitely receive it, or you don't care whether you want to guarantee When the other party receives your message, it can send the message directly like UDP." This can be regarded as another solution to the purpose of "three-way handshake".

Why does it have to be three instead of two?

A greeted B on the road, and the sentence was blown away because of the wind, and then A said hello again, and B heard and responded. At this time, whether it is three handshakes or two handshakes, two people can communicate happily. After 0.1 seconds, the two waved goodbye four times. At this time, the words that were blown away by the wind reached B's ears again. B thought that A wanted to communicate with him again, so he responded in response. (The problem arises) If two handshakes are used, B will decide that A wants to communicate with him, so he keeps waiting and wastes his feelings. But if the three-way handshake is used, B waits for a while and finds that A has not responded, and he thinks that A has left and then he also leaves!
This is very clear. In fact, the third step is to prevent B from wasting his time by waiting, not to ensure that A can correctly respond to B's information.

TCP three-way handshake

  • The client sends a request message to establish a C to S connection with the synchronization flag (SYN) set to 1. Sequence Number(seq) is x, then enter the SYN_SEND state, waiting for the server to confirm
  • The server returns an acknowledgment data message, sets ACK to x+1(seq+1), also sets SYN to 1, and seq to y, requests to establish a connection from S to C, and the server enters the SYN_RCVD state.
  • The client returns an acknowledgment data message, the ACK is incremented, and it is set to y+1. At this time, the connection between the two parties is successfully established, and both the client and the server enter the ESTABLISHED (TCP connection is successful) state.

TCP waved four times

TCP waved four times

  • The first breakup: host 1 (can be the client or the server), set the Sequence Number and Acknowledgment Number, and send a FIN segment to host 2; at this time, host 1 enters the FIN_WAIT_1 state; this means that host 1 There is no data to send to host 2;
  • The second breakup: Host 2 receives the FIN segment sent by Host 1, and returns an ACK segment to Host 1. The Acknowledgment Number is the Sequence Number plus 1; Host 1 enters the FIN_WAIT_2 state; Host 2 tells Host 1, "I" agree" your request to close;
  • The third breakup: host 2 sends a FIN segment to host 1, requesting to close the connection, and host 2 enters the LAST_ACK state;
  • The fourth breakup: host 1 receives the FIN segment sent by host 2, sends an ACK segment to host 2, and then host 1 enters the TIME_WAIT state; after host 2 receives the ACK segment from host 1, it closes the connection ; At this point, if host 1 still does not receive a reply after waiting for 2MSL, it proves that the server side has been shut down normally. Well, host 1 can also close the connection.

Why four times?

The TCP connection is full-duplex, and each end can send and receive data at the same time. When closing, both ends must close their respective channels in both directions, which is equivalent to closing four in total.

Step 4 Why does the client wait for 2MSL?

First of all, MSL (Maximum Segment Life) is the limitation of TCP on the lifetime of a TCP Segment.
After the client sends out the ACK confirming the shutdown of the server, it has no way of knowing whether the other party has received the message, so it needs to wait for a while. If the server does not receive the shutdown message, it will re-send the FIN message, so that the client can Knowing that the previous message was lost, it needs to be sent again; if it does not receive the FIN resend message during the waiting period, it means that it has indeed received the disconnected message and has been disconnected.
This waiting time is at least: the client's timeout + FIN transmission time. In order to ensure reliability, a more conservative waiting time of 2MSL is adopted.

UDP protocol

The UDP protocol is not as stable as the TCP protocol, because it does not establish a connection and does not send in sequence, and there may be packet loss, which may cause errors in the transmitted data.

However, there are gains and losses. UDP is more efficient, because the UDP header contains few bytes, which consumes less load than TCP. At the same time, it can also achieve two-way communication. Regardless of the accuracy of message delivery, it is only responsible for brainless sending.

UDP serves many well-known application layer protocols, such as NFS (Network File System), SNMP (Simple Network Management Protocol)

UDP is generally used in scenarios with strong fault tolerance, such as IP telephony and network video.

HTTP connection

The HTTP protocol is the Hypertext Transfer Protocol (Hypertext Transfer Protocol), which is the basis of Web networking and one of the commonly used protocols for mobile phone networking. The HTTP protocol is an application built on top of the TCP protocol.
The most notable feature of HTTP connections is that each request sent by the client requires the server to send back a response, and the connection will be actively released after the request ends. The process from establishing a connection to closing it is called a "one-time connection".
1) In HTTP 1.0, each request of the client requires a separate connection to be established, and the connection is automatically released after the request is processed.

2) In HTTP 1.1, multiple requests can be processed in one connection, and multiple requests can be overlapped, without waiting for one request to end before sending the next request.

Since HTTP will actively release the connection after each request, the HTTP connection is a "short connection". To maintain the online state of the client program, it is necessary to continuously initiate connection requests to the server. The usual practice is that even if there is no need to obtain any data, the client also keeps sending a "keep-connect" request to the server at regular intervals, and the server replies to the client after receiving the request, indicating that it knows the client " online". If the server cannot receive the client's request for a long time, it is considered that the client is "offline". If the client cannot receive the reply from the server for a long time, it is considered that the network has been disconnected.

1. Introduction to HTTP

HTTP is an object-oriented protocol belonging to the application layer. Due to its simplicity and speed, it is suitable for distributed hypermedia information systems. It was proposed in 1990, and after several years of use and development, it has been continuously improved and expanded.

Main features of the HTTP protocol

  1. Support C/S (client/server) mode.
  2. Simple and fast: When a client requests a service from the server, it only needs to transmit the request method and path. Commonly used request methods are GET, HEAD, and POST. Each method specifies the type of contact between the client and the server. Because the HTTP protocol is simple, the program scale of the HTTP server is small, so the communication speed is fast.
  3. Flexible: HTTP allows the transfer of data objects of any type. The type being transferred is marked by Content-Type.
  4. Connectionless: The meaning of connectionless is to limit the processing of only one request per connection. After the server processes the client's request and receives the client's response, it disconnects. In this way, transmission time can be saved.
  5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capability for transaction processing. The lack of state means that if previous information is required for subsequent processing, it must be retransmitted, potentially resulting in an increased amount of data transferred per connection. On the other hand, the server responds faster when it does not need the previous information.

The format of the HTTP URL is as follows

http://host[":"port][abs_path]

http means to locate network resources through the HTTP protocol; host means a legitimate Internet host domain name or IP address; port specifies a port number, if it is empty, the default port 80 is used; abs_path specifies the URI of the requested resource (any available resource on the Web) .
HTTP has two types of messages: request message and response message. Let's take a look at the request message first.

 

request header

Generally speaking, an HTTP request message consists of four parts: request line, request header, blank line, and request data.

request line

The request line consists of the request method, the URL field, and the version of the HTTP protocol, in the following format:

Method Request-URI HTTP-Version CRLF

Where Method represents the request method; Request-URI is a Uniform Resource Identifier; HTTP-Version represents the requested HTTP protocol version; CRLF represents carriage return and line feed (except for the trailing CRLF, separate CR or LF characters are not allowed) .

There are 8 HTTP request methods, namely GET, POST, DELETE, PUT, HEAD, TRACE, CONNECT, OPTIONS. Among them, PUT, DELETE, POST, and GET correspond to additions, deletions, and changes, respectively. For mobile development, the most commonly used ones are POST and GET.

  1. GET: request to obtain the resource identified by the Request-URI
  2. POST: Append new data to the resource identified by the Request-URI
  3. HEAD: Response message header for the request to obtain the resource identified by the Request-URI
  4. PUT: Request the server to store a resource and use the Request-URI as its identifier
  5. DELETE: Requests the server to delete the resource identified by the Request-URI
  6. TRACE: Request the server to send back the received request information, mainly for testing or diagnosis
  7. CONNECT: The HTTP/1.1 protocol is reserved for proxy servers that can change the connection to pipe mode.
  8. OPTIONS : Request to query the performance of the server, or to query the options and requirements related to the resource

request header

After the request line there will be zero or more request headers, each of which contains a name and a value, separated by ":". The request header will be sent with a blank line, carriage return and line feed, notifying the server that there will be no request headers below. Regarding the request header, a unified explanation will be given in the following section of the message header.

request data

Request data is not used in GET method, but in POST method. The POST method is suitable for occasions where the customer needs to fill in the form. The most commonly used request headers related to the request data are Content-Type and Content-Length.

HTTP response message

Let's first look at the general format of the response message:

 

response header

An HTTP response message consists of a status line, a message header, a blank line, and a response body. As will be mentioned later in the response header, the response body is the content of the resource returned by the server. Let's take a look at the status line first.

status line

1. The format of the status line is as follows:

HTTP-Version Status-Code Reason-Phrase CRLF

Among them, HTTP-Version represents the version of the server HTTP protocol; Status-Code represents the response status code sent back by the server; Reason-Phrase represents the textual description of the status code.
The status code consists of three digits, the first digit defines the category of the response, and there are five possible values:

  • 100~199: Indication information, indicating that the request has been received, continue processing
  • 200~299: The request is successful, indicating that the request has been successfully received, understood, and accepted
  • 300~399: redirection, further operations must be performed to complete the request
  • 400~499: Client error, the request has a syntax error or the request cannot be fulfilled
  • 500~599: Server-side error, the server failed to implement a legitimate request

Common status codes are as follows:

  • 200 OK: The client request was successful
  • 400 Bad Request: The client request has a syntax error and cannot be understood by the server
  • 401 Unauthorized: The request is not authorized, this status code must be used with the WWW-Authenticate header field
  • 403 Forbidden: The server received the request, but refused to serve
  • 500 Internal Server Error: An unexpected error occurred on the server
  • 503 Server Unavailable: The server is currently unable to process the client's request and may return to normal after a period of time

SOCKET principle

Socket concept

Socket is the cornerstone of communication and the basic operation unit of network communication supporting TCP/IP protocol. It is an abstract representation of endpoints in the network communication process, and contains five kinds of information necessary for network communication: the protocol used for the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol of the remote process. port.

When the application layer communicates data through the transport layer, TCP encounters the problem of providing concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transfer data over the same TCP protocol port. In order to distinguish different application processes and connections, many computer operating systems provide socket (Socket) interfaces for application programs to interact with the TCP/IP protocol. The application layer can distinguish the communication from different application processes or network connections through the Socket interface with the transport layer, and realize the concurrent service of data transmission.

establish socket connection

Establishing a Socket connection requires at least a pair of sockets, one of which runs on the client side, called ClientSocket, and the other runs on the server side, called ServerSocket.

The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.

  • Server monitoring: The server-side socket does not locate a specific client socket, but is in a state of waiting for a connection, monitors the network status in real time, and waits for a client's connection request.

  • Client request: refers to the connection request made by the client's socket, and the target to be connected is the socket of the server. To do this, the client's socket must first describe the server's socket to which it wants to connect, point out the address and port number of the server-side socket, and then make a connection request to the server-side socket.

  • Connection confirmation: When the server-side socket listens or receives the connection request of the client-side socket, it responds to the request of the client-side socket, establishes a new thread, and sends the description of the server-side socket. To the client, once the client confirms this description, the two sides formally establish the connection. The server-side socket continues to be in the listening state and continues to receive connection requests from other client-side sockets.

SOCKET connection vs TCP connection

When creating a Socket connection, you can specify the transport layer protocol used. Socket can support different transport layer protocols (TCP or UDP). When using the TCP protocol to connect, the Socket connection is a TCP connection.

Socket connection and HTTP connection

Since the Socket connection is usually a TCP connection, once the Socket connection is established, the two communicating parties can start sending data content to each other until the connection between the two parties is disconnected. However, in practical network applications, the communication between the client and the server often needs to pass through multiple intermediate nodes, such as routers, gateways, firewalls, etc. Most firewalls will close the connection that is inactive for a long time by default, resulting in the disconnection of the socket connection. connection, so you need to tell the network by polling that the connection is active.

The HTTP connection uses the "request-response" method, which not only needs to establish a connection when requesting, but also requires the client to send a request to the server before the server can reply to data.

In many cases, the server side needs to actively push data to the client side to keep the real-time and synchronization of the client and server data. At this time, if the two parties establish a Socket connection, the server can directly transmit the data to the client; if the two parties establish an HTTP connection, the server needs to wait for the client to send a request before sending the data back to the client. Therefore, The client sends a connection request to the server periodically, which not only keeps it online, but also “asks” the server if there is new data, and if so, transmits the data to the client. TCP (Transmission Control Protocol) Transmission Control Protocol

TCP is a host-to-host layer transmission control protocol, providing reliable connection services, using three-way handshake confirmation to establish a connection:

The bit code is the tcp flag bit, and there are 6 kinds of signs: SYN (synchronous connection establishment) ACK (acknowledgement confirmation) PSH (push transmission) FIN (finish end) RST (reset reset) URG (urgent emergency)
Sequence number (sequence number) Acknowledge number

After reading the document pushed by Jiguang, Jiguang's long-term connection is a long-term connection established by using the TCP protocol. The
TCP long-term connection is implemented with its own internal implementation logic to complete the long-term connection.

Maintain the connection state as much as possible with the least cost. For policy reasons, it is not always possible to reconnect immediately after each disconnection.

There is also a mechanism of keepalive connections in Http, which can still maintain the connection after data transmission. When the client needs to obtain data again, it directly uses the connection that has just been idle without shaking hands again.



Author: YoungerDev
Link: https://www.jianshu.com/p/e03b9126b262
Source : Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325166143&siteId=291194637