One, foreword

Alamofire is a set of network request tripartite library written in Swift, equivalent to AFNetWorking in OC. Before we begin with Alamofire, let’s review some of the basics of networking so we can better understand the network request framework.

OSI seven-layer network architecture

1, an overview of

Internet protocols are divided into OSI seven or TCP/IP five or TCP/IP four layers according to their functions. Each layer runs a different protocol. The diagram below:

The corresponding functions and protocols of each layer are shown in the following table:

OSI seven-layer network model TCP/IP four-tier conceptual model function Corresponding protocol
Application Layer The application layer File transfer, E-mail, file services, virtual terminals HTTP, TFTP, FTP, NFS, WAIS, SMTP, Telnet
Presentation Layer The application layer Data formatting, code conversion, data encryption Telnet, Rlogin, SNMP, Gopher
Session Layer The application layer To disengage or establish connections with other contacts SMTP, DNSr
Transport Layer The transport layer Provide an end-to-end interface TCP, UDP
Network Layer The network layer Select a route for the packet SMTP, DNSr
Data Link Layer Network interface layer Transmission of addressed frames, error detection function FDDI, Ethernet, Arpanet, PDN, SLIP, PPP
Physical Layer Network interface layer The transmission of data over physical media as binary data IEEE 802.1a, IEEE 802.2 to IEEE 802.11

The process by which a client sends a request to a receiver:

2. Specific introduction

1) Physical layer

The physical layer is connected through optical fibers, cables, etc., and sends high and low voltage (electrical signals) based on the characteristics of electrical appliances. High voltage corresponds to the number 1, low voltage corresponds to the number 0, and transmits a bunch of binary bits 001010100.

2) Data Link layer

It is meaningless to simply use 0 and 1 of electrical signals in the physical layer. It is necessary to specify how many bits of electrical signals are in a group and what the meaning of each group is. The data link layer is to group electrical signals. The data link layer provides reliable transport over unreliable physical media. The functions of this layer include: physical address addressing, data framing, flow control, data error detection, retransmission, etc. At this level, the units of data are called frames. Data link layer protocols include SDLC, HDLC, PPP, and STP.

3) Network layer

The task of the network layer is to select the appropriate internetwork routing and switching nodes to ensure the timely transmission of data. The network layer consists of packets of frames provided by the data link layer. The packets contain the network layer packet header, which contains the logical address information – the network address of the source site and destination site address. Routers are the main devices at the network layer. Network layer can also realize congestion control, Internet interconnection and other functions. At this level, the units of data are called packets. Network layer protocols include IP, IPX, RIP, and OSPF.

4) Transport layer

The IP of the network layer helps us distinguish the subnet, and the MAC of the Ethernet layer helps us find the host. Then how to identify the application program on this host requires the port, which is the number associated with the application program and the network card. The transport layer is used to establish port-to-port communication. Ports range from 0 to 65535 and 0 to 1023 are used by the system. #### (1) TCP protocol: reliable transmission. TCP packets have no length limit and can be infinitely long in theory. However, to ensure network efficiency, the length of TCP packets usually does not exceed the length of IP packets to ensure that a single TCP packet does not need to be split.

  • Source and destination ports: A combination of an IP address and port is called ** “socket” or “endpoint” **. Therefore, the source IP address and destination IP address in THE IP protocol form a “pair” of sockets (sending socket and receiving socket).

  • Serial number: Each “first byte in TCP packet segment” is assigned a serial number (increasing). The sequence number indicates different meanings based on whether the SYN in the control flag is 1. SYN = 1: The connection is being established, and the ISN is the initial sequence number. When data transfer begins, the ISN + 1 is the sequence number of the first byte of the data. SYN = 0: indicates the sequence number of the first byte in the data part of the current packet segment.

  • Acknowledgement number (used by TCP returned packets) : The acknowledgement number is also called the ACK number or ACK field. The confirmation number contains the following values: the next serial number that the sender of the confirmation number wishes to receive. Once a connection is established, the ACK value is always 1.

  • Data offset: length of the header of a TCP packet segment, expressed in Word (4 bytes). It is the offset from the start of a TCP packet segment to the start of a TCP packet segment. The largest number that can be represented in 4 bytes is 15, so the largest header is 60 bytes.

  • Reserved field: Reserved for future use, must be 0.

  • Control flags: There are six control flags. SYN/ACK and FIN/ACK are used during connection establishment and disconnection. URG: When set to 1, it indicates that the emergency pointer field is valid. ACK: Confirm that the serial number field is valid. PSH: The receiver immediately sends the packet segment to the application layer. RST: reestablishes the connection. SYN: indicates the synchronization sequence number, which is used to establish a connection. FIN: The sender stops sending data.

  • Window size: the amount of data allowed to be sent. Control the speed at which the other person can send data by telling them how many bytes they can hold in their buffer.

  • Checksum: indicates the result of CRC operation on the TCP header and data. After receiving the data, the receiving end performs CRC operations on the header and data of the received TCP packet segment and compares them with the verification in the TCP header to ensure that the data is not damaged during transmission.

  • Emergency pointer: valid only if URG=1. Its value is an offset that is added to the value in the ordinal field to obtain the ordinal number of the last byte of emergency data.

  • Optional field: The most common optional field is Maximum Segment Size (MSS), which indicates the Maximum packet Size. The communication parties usually specify this parameter in the first packet Segment of the connection. (Only in SYN packets)

Three handshakes and four waves:

#### (2) UDP protocol: unreliable transmission, “header” part of a total of only 8 bytes, total length of no more than 65,535 bytes, just into an IP packet.

5) Session Layer

This layer can also be called the meeting layer or the conversation layer. At the session layer and above, the units of data transmission are no longer individually named, but collectively referred to as messages. The session layer does not participate in the specific transport and provides mechanisms for establishing and maintaining communication between applications, including access validation and session management. For example, the server authenticates the user login by the session layer.

6) Presentation

This layer addresses the syntactic representation of the advocacy message. It converts the data to be exchanged from an abstract syntax suitable for a user to a transport syntax suitable for use within the OSI system. That is, to provide formatted presentation and transformation data services. The presentation layer is responsible for data compression and decompression, encryption and decryption.

7) Application Layer

The application layer provides the interface for the operating system or network applications to access network services. Application layer protocols include Telnet, FTP, HTTP, and SNMP.

The above summary refers to and partly extracts the following articles, thanks very much to the following authors for sharing! :

1. Network Protocol (OSI Seven-Layer Protocol) by Taibai Jinxing

2. Illustrated HTTP by Ueno

3, Red_Code “TCP protocol details”

4. “TCP Introduction and Example Explanation”

Reprint please note the original source, shall not be used for commercial communication – any more