A Brief Description of TCP Protocol


This article briefly describes the general content of the agreement, specific please Baidu Oh!
TCP protocol is divided into four layers [some are divided into seven layers]
The picture is from the data sheet of the Texas Instrument Network Card chip.
A Brief Description of TCP Protocol

A Brief Description of TCP Protocol

1. Data Link Layer
2. Network Layer
3. Transport Layer
4. Application Layer
The important protocol of data link layer is ARP protocol. In fact, ARP is a driver written in C language. The important thing of data link layer is the driver of network card chip, which contains a large number of working registers. The graph is as follows

A Brief Description of TCP Protocol
The chip diagram is from the information of Taiwan semiconductor manufacturers! [Specific chip diagrams can be obtained by contacting myself] The chip is used in the Ethernet controller of the embedded system, and the data receiving and receiving function can be realized by writing driver. [tcp, UDP is only a protocol, the lower level also depends on the driver of the chip, the students who have written driver should be more clear, the upper application is used. The functions provided by the lower level do it.)

The internal structure includes physical transceiver, namely PHY, Mac, processor interface, memory management, memory random register SRAM, MII management register, including EEPROM erasable register, etc.

External Hardware Connection Diagram of Wind Card Chip

A Brief Description of TCP Protocol

Register is a logic circuit with storage function. Its drive depends on the clock pulse square wave signal to drive its work. Its output is related to the input and the state at that time. Unlike combinational logic circuit, registers are composed of a large number of triggers. A trigger has the function of memory of one bit binary. Its output and input are related to each other. The trigger consists of gate circuits (conventional gates such as and gates, or gates, non-gates, and or gates, and non-gates, that is, circuits that implement logical operations such as and, or, and, or, and, or, and, or, and, or, etc.). These circuits are constructed by semiconductor diodes or triodes or field effect transistor elements. All in all, remember that registers have data memory function.

Functions of chips

A Brief Description of TCP Protocol
It supports byte, single-byte, double-byte instructions to access internal data, integrates 10,100 transceivers, supports IEEE standards, supports wake-up, integrates 4k-sized double-byte registers, and EEPROM has built-in data when it leaves the factory. Users can read its data, compatible with 3.3V and 5V works. For voltage, the chip has a 32-bit data bus, which supports 4 bytes of access.

Processor Interface Description:
A Brief Description of TCP Protocol

A Brief Description of TCP Protocol

A Brief Description of TCP Protocol
Partial pins have output and input functions, i.e. IO, I for input, O for input and P for power supply.
Read instruction pin, write instruction pin, data bus pin, data bus can input voltage or output voltage [data], including chip enabling pin, and address bus pin for selecting chip

Clock Pulse Interface

A Brief Description of TCP Protocol

Read-write operation of chip
Generally speaking, network card chips will connect the corresponding processor chips such as SCM S3C2440 or STM32. Network card chips generally support 32-bit or 16-bit data bus. The data bus pins of processors will connect the data pins of network card chips such as SD0-SD31. The most important of these are processor pins such as CMD and IOR.# IOW, AEN, CMD input different signals [high or low level will choose the data bus to transmit register address or registered data]

Write operation
Generally, the address of the register is specified, and then the register data is given. Firstly, the TXSRAM register is specified, and then the data is written to the register. By controlling the level signal of the pin CMD port, the data to be sent is written to the data bus. Then, the TX controller can register the data to be sent, and the sending status of the data will be recorded to the TX state register. Registers and network status registers. TXSRAM is generally associated with MWCMD. Specify the register when the operation is written. When sending again, the length and size of the data to be sent should also be written into the registers of TXPLH and TXPLL.

Read operation
The same is to specify RXSRAM registers, addresses are specified by MRCMD. When the network card chip receives data, the data will be stored in the RXSRAM registers. By specifying the registers, the data will be exported from the data bus SD to the processor [i.e. the single chip computer]. The program will continuously read whether the RXSRAM registers have data or not, and then directly from the number of data. The output data can be obtained from the bus.

Reading and writing are usually connected with data bus, and without data bus, they must be processed in read and write sequence.
The sending and receiving of data must know the MAC address of the other party. The MAC address is usually written in the corresponding register when it is out of the factory, so the data will get the MAC address of another network card chip before transmission. The MAC address is usually 6 bytes, but usually the IP of the transmitting party is exchanged for the MAC address of the other party to return. The process is address resolution, or ARP. [the request and response process of ARP can find data by itself]

It is concluded that the driver of data link layer needs to know the MAC address of the other party and the data to be sent when sending and receiving.

The driver of network card chips mainly provides services for the upper layer. Anyway, they are all reading and writing data. It’s OK to know which MAC (which network card) they want to read and write.

Network layer:
Responsible for the data transmission function, related important protocols such as ICMP, IP protocol, etc., its transmission depends on the network card driver to provide.

Transport layer:
TCP, UDP is based on byte stream transmission, one is based on data transmission, byte stream transmission is stable, using a response mechanism, with the function of overtime retransmit, so that data can reach the receiver completely, while UDP is the opposite.
For TCP programming operating system, socket interface is provided for upper layer. Based on this interface, a series of application layer protocols are developed, such as http, FTP and so on.
TCP protocol description:
TCP will be packaged and encapsulated by TCP when transmitting data. [Anyway, each layer is encapsulated, and the corresponding receiving end is the distribution layer parsing]. Two important parameters of this layer are ip, port, address and port number. [User-specified, generally up to 65535, some ports are used by the system, and some of them are those ports. Well-known software ports such as 80, 6379, 3306 are used.) IP goes without saying, because ARP drivers need to know each other’s MAC address at runtime. This IP is the other’s ip. The port number is used to distinguish which application is http, such as web server or other server, so when receiving, it depends on the port. Port number to distinguish.

TCP encapsulates destination ip, port, source ip, source port, 32-bit serial number, 32-bit confirmation number, and some flag bits such as SYN, ACK, FIN, PUSH, RST. Detailed drawings are as follows

A Brief Description of TCP Protocol

SYN: A connection flag bit, indicating that the sender initiated a connection request, generally initiated by the client, such as sending a SYN (SYN_SENT status at this time) through the socket interface’s connection function, at which time if the server responded, it would send a confirmation flag bit, ACK.
To confirm the serial number, one is added to the 32 serial number to confirm that the server is in the SYN_RVD state at this time. If the client also confirms that the ACK response flag bit is in the ESTABLISHED state at this time, both sides can send messages to each other. The above process is a typical three-time handshake [some people shake hands four times]. ]
Pass the test [test the picture on my github]

A Brief Description of TCP Protocol

So when you call [if your PHP calls the socket connection function, the client will send a SYN connection request message, the SYN flag bit will send to the server, of course, the server will start first, mainly when the listen function is called into the LISTEN state. [You can see the state of the server-tcp through the workerma with talent command. The socket API of n, swoole or C should be clear. When the low-level driver runs, the server will send a SYN response, ACK+1, when the client confirms that the connection is successful. If the port does not exist or the port is occupied, the connection will fail.

When the client calls the close function, the client sends a FIN termination message. At this time, the client is in FIN_WAIT1 state. After the server confirms, the ACK reply confirmation message is sent. At this time, the server is in CLOSE_WAIT state, and the client confirms that it is in FIN_WAIT2 state. At present, the client is in FIN_WAIT2 state. The server can send data because it hasn’t been turned off yet. 】 If the server also sends FIN end message, then the server side is in the state of LAST_LOCK, if the client confirms that the server is in the state of CLOSED, and the client is in the state of TIME_WAIT. When the server sends the FIN message, the client is disconnected from receiving it, and then it is connected to the network, and it can receive the message again.

By analyzing [the test pictures I sent, you can test Baidu or contact me if you don’t know it], we conclude that TCP confirmation is based on the response mechanism. Whether it is connection, shutdown or sending data, you must get the other party’s promised message for confirmation and then proceed to the next operation.

Address description:
The addresses provided by the operating system are general addresses, as well as IPV4, IPV6, UNIX addresses.
Address generally provides IP, PORT, FAMILY, i.e. ip, port number and address family, as well as related protocol families and data stream types. There are mainly byte streams such as SOCK_STREAM and datagram services such as SOCK_DGRAM [UDP protocol is that]

When you have a clear understanding of TCP/IP protocol, the application layer protocols such as HTTP, FTP, TELNET are simple.
Driver engineers ignore these protocols and operate the chip driver of the network card to complete LAN transmission.

Through the above simple analysis, you should understand a communication process of TCP/IP protocol, including the low-level driver also made a simple explanation.
Specifically, you can search for additional information on your own.

Please indicate the source when reprinting this article! Thank you.

Recommended Today

Resolving Time Zone Errors with Docker

1. The correct time zone in China is set to CST, i.e.China Standard TimeIn usedockerstart-upJenkinsWhen mirroring, the settings for mirroring are usuallyCoordinated Universal Time。 So add parameters at startup -v /etc/localtime:/etc/localtimeMount the local time zone to the mirror, so that the mirror gets the correct time zone. Then go to Jenkins and fill in the […]