Transport layer
- Transport layer overview
- UDP protocol
- TCP protocol
- Sockets and socket programming
Transport layer overview
Programmers generally use the interface provided by the transport layer for network programming.
The transport layer belongs to the lowest layer of the user part and the highest layer of communication.
The role of the transport layer : to manage end-to-end communication connections.
The problem solved by the transport layer is how to communicate with two terminal devices connected through a virtual interconnection network. That is, the communication between processes, which is called network communication here.
The process communication methods learned in the operating system are: shared memory, Unix domain sockets.
the difference:
Network communication: cross-network, cross-device process communication
Shared memory, socket: can only realize the communication between processes on a single machine
The concept of
port Use port (Port) to mark different network processes
Port (Port) uses 16-bit representation (0~65535)
UDP protocol
The concept of UDP protocol:
UDP (User Datagram Protocol: User Datagram Protocol)
UDP is a very simple protocol
UDP protocol header:
Features of UDP protocol:
- UDP is a connectionless protocol . When sending data between computers, there is no need to establish a connection in advance, and send it directly when you want to send it.
- UDP cannot guarantee reliable delivery of data . Send as you want. There is no guarantee that the data will be lost in the network.
- UDP is for message transmission . Datagram refers to a complete data transmitted from the application layer. For datagrams transmitted by the application, the datagrams passed down by the upper layer are not merged, split, or processed.
Therefore, the datagram directly determines the length of the UDP datagram. - UDP has no congestion control . Can't perceive whether the network is congested, and try to transmit data even if congested
- The header overhead of UDP is very small .
TCP protocol
The concept of TCP protocol:
TCP (Transmission Control Protocol: Transmission Control Protocol) The
TCP protocol is a very complex protocol in the computer network. The
TCP protocol header:
- Serial number: 0~2^32-1; one byte, one serial number; the serial number of the first byte of data.
- Acknowledgement number: 0~2^32-1; one byte, one serial number; the serial number of the first byte of the expected data. If the confirmation number is N, it means that the data of N-1 sequence number has been received.
- Data offset: 4 digits: 0~15, unit: 32-bit word; the distance of the data from the header.
- TCP mark: occupies 6 bits, each has a different meaning.
- Window: occupies 16 bits: 0~2^16-1; the window indicates the amount of data that the other party is allowed to send.
- Urgent pointer: Urgent data (start when URG=1); specify the location of the urgent data in the message.
- TCP options: up to 40 bytes; support future expansion.
Features of TCP protocol:
- TCP is a connection-oriented protocol . The connection must be established before data can be sent.
- A TCP connection has two ends (point-to-point communication).
- TCP provides reliable transmission services .
- The TCP protocol provides full-duplex communication . Full duplex means that both computers can send data to or receive data from the connection at the same time.
- TCP is a byte stream-oriented protocol . Byte stream refers to the sequence of bytes flowing into or out of the process. The TCP protocol treats the data transmitted by the application layer as a series of byte streams. It is possible to split into two transmissions, it is possible to merge or split
Reliable transmission of TCP protocol
Basic principles of reliable transmission
1. Stop waiting for the protocol
. After the sender finishes sending a message, it stops sending a new message, waits for the receiver to confirm, and then sends a new message after confirming the completion. The sender and receiver are both in a stop-wait-stop process.
What went wrong: The sent message was lost on the road; the confirmed message was lost on the road; the confirmed message arrived a long time ago.
Every time a message is sent, a timer needs to be set.
In these cases, the stop-and-wait protocol can ensure reliable transmission through timeout retransmission.
The stop-waiting protocol is the simplest reliable transmission protocol. The
stop-waiting protocol is not efficient in channel utilization
2. Continuous ARQ protocol
ARQ (Automatic Repeat reQuest: Automatic Repeat reQuest), which can realize batch sending and confirmation.
Cumulative confirmation: If a message is received, it means that all previous messages have been received, and the sliding window is pushed forward.
Reliable transmission of TCP protocol
TCP's reliable transmission is based on the continuous ARQ protocol.
TCP's sliding window
selects retransmission in bytes . You need to specify the bytes that need to be retransmitted, and specify the boundary.
Each byte has a unique 32-bit serial number.
TCP protocol flow control
Flow control means that the sender should not send too fast.
Flow control is achieved by using a sliding window.
When the receiver informs the sender that the next message with a window of 100 is lost, the sender will wait forever because it has not received the message, causing death. Why does the lock happen, because the reliable transmission of the TCP protocol is only reliable for data transmission and not reliable for window messages. Solution: stick to the timer.
Persistence timer
When a message with a window of 0 is received, the persistence timer is started.
The persistence timer sends a window detection message at regular intervals.
The TCP protocol uses a sliding window to achieve flow control and
adhere to the role of timers
Congestion control of TCP protocol
The difference between flow control and congestion control:
Flow control considers point-to-point traffic control.
Congestion control considers the entire network, which is a global consideration.
Method of judging congestion : the message timeout is considered as congestion.
Slow start algorithm: exponential growth.
Congestion avoidance algorithm: linear growth.
Three-way handshake of TCP connection (connection establishment)
Key TCP flags:
ACK: acknowledgment bit, ACK=1, the acknowledgment number takes effect
SYN: synchronization bit, SYN=1 means connection request message
FIN: termination bit, FIN=1 means release connection
The three-way handshake process:
establish a connection, synchronize their serial numbers
Why does the sender send out the third confirmation message?
The connection request message that has been invalid is transmitted to the other party, which will cause an error. Two TCP connections may be caused when retransmission is timed out.
Four waves of TCP connection (connection release)
Waiting timer (TIME-WAIT): The
waiting time is 2MSL
MSL (Max Segment Lifetime): The longest segment life
MSL is recommended to be set to 2 minutes
Why wait for 2MSL?
1. If the last message is not confirmed, it is impossible to ensure that the sender's ACK can reach the receiver. If it is not received within 2MSL, the receiver will retransmit and repeat the third wave.
2. Make sure that all messages currently connected have expired
Four timers of TCP protocol
Sockets and socket programming
Use Port to mark different network processes.
Port is represented by 16 bits (0~65535).
Socket is a combination of IP and port number. An IP can have multiple sockets because it can be combined with different port numbers.
Socket is an abstract concept that represents one end of a TCP connection.
Data can be sent or received through sockets.
The process of TCP connection between client and server:
code:
server.py
# -*- encoding=UTF-8 -*-
import socket
def server():
# 创建socket
s = socket.socket()
host = '127.0.0.1'
post = 6666
# 绑定套接字
s.bind((host,post))
# 监听
s.listen(5)
while True:
c, addr = s.accept()
print('connect addr:', addr)
c.send('welcome to server!')
c.close()
if __name__ == '__main__':
server()
client.py
# -*- encoding=UTF-8 -*-
import socket
def client(i):
# 创建socket
s = socket.socket()
# 连接套接字
s.connect(('127.0.0.1', 6666))
print('Receive message: %s, client: %d'%(s.recv(1024), i))
s.close()
if __name__ == '__main__':
for i in range(10):
client(i)
The network socket of the computer network is compared with the domain socket of the operating system. Compare the
network socket: whether it is across computers or the same computer, if you use a network socket for data transmission, the data will pass through the following protocol stack. To return to the user interface.
Domain sockets are used for data transmission through domain socket files. Using this method, data does not need to pass through the protocol stack. The domain socket is recommended for stand-alone communication, which has simple processing flow and low system consumption.