This article takes you through multiplexing and demultiplexing

Write in front: here is Xiao Wang's growth log, an ordinary college student who wants to share his study notes after learning, record his growth trajectory, and help those who may need it. Usually the blog content is mainly some systems Study notes, practical project notes, some technical explorations and some of my own thinking. Everyone is welcome to pay attention, I will carefully read every comment you like and follow. If you have any questions, please let me know. I will do my best to help everyone and create a beautiful environment for CSDN.

Recently I'm looking at the computer network, this is a study note, because it is self-study, the level is limited, and it is not necessarily very deep, but it is guaranteed that the things sent out must be thought out and sorted out by myself. Every sentence has been verified. Guidance, if there are errors, please spray lightly.

Pre-knowledge

How the process gets data from the network

First we understand the process by which the process receives data from the network:

At the destination host, the transport layer needs to receive message segments from the network layer below it.

The transport layer is responsible for delivering the data in these segments to the specified socket of the target process (and a process may have multiple sockets)

Therefore, the socket (Socket) acts as a gateway for transferring data from the process to the network and transferring data from the process to the network.
As shown below

Insert picture description here

Transport layer message structure

As we can see each transport layer packet header has two fields - source port number and destination port number .

On the host, each socket corresponds to a process, and each socket can be assigned a port number .

So when the segment reaches the host, the transport layer checks the destination port number in the segment and directs it to the corresponding socket (multiplexing), and then the data in the segment enters the person through the socket The process to which it is connected.

Overview

Demultiplexing

Each transport layer segment has several fields (that is, the source port number and destination port number above). At the receiving end, the transport layer checks these fields, identifies the receiving socket and compares it with the socket's identification information, and if it matches, directs the message segment to the socket.

Multiplexing

The source host collects data blocks from different sockets, and encapsulates the header information for each data block (encapsulating the source port number and destination port number, which will be used for decomposition later) to generate a message segment, and then reports The text is passed to the network layer.

Give a chestnut

We use a chestnut in the book to illustrate what is demultiplexing and multiplexing:

There are a total of two families (two end systems) in this chestnut , each family has 7 children (process ), the children of each family will write a letter to each other every week , for example, the boss to the boss, the second to Old and written ( letters and characters on the envelope symbolize application-level messages ).

We let the first small B and the second old A be responsible for sending and receiving mail within the home (the transport layer receives messages from the IP layer and delivers them to the corresponding process) . Obviously the external postal system acts as an information transmission channel between the two end systems.

Each child is identified by their name. When little B receives a batch of letters from the postman (IP layer) and delivers the letters to his siblings (process) by checking the recipient's name (destination port number on the message), he executes It is a decomposition operation.

When old A collects letters from siblings (processes) and delivers them to the postman (IP layer), she performs a multiplexing operation.

What is the difference between multiplexing and demultiplexing in UDP and TCP

The main difference is the scoket:

UDP socket-connectionless demultiplexing and multiplexing

When we create a UDP socket, the transport layer will automatically or we artificially bind a port between 1024-65535 (the remaining ports are well-known ports, which are reserved for some well-known application layer protocols).

UDP sockets to by the tuple comprehensive identification of

  • The two-tuple contains a destination IP address and a destination port number .

    Therefore, if two UDP segments have different source IP addresses or source port numbers, but have the same destination IP address and destination port number , then the two segments will be directed to the same destination socket The same purpose process . (Multiplexing)

  • The source port number is used as part of the "return address" to fill in the destination port of the reply message, which is obvious

Insert picture description here

TCP socket-connection-oriented multiplexing and demultiplexing

There is a slight difference between TCP socket connection and UDP socket : TCP socket is identified by a four-tuple (source IP address, source port number, destination IP address, destination port number) .

Therefore, because four values ​​are used to locate the socket , two TCP segments from different IPs with the same source port will be directed to two different sockets , and vice versa. (Multiplexing)

The server host can support many parallel TCP sockets, each socket is associated with a process, and each socket is identified by its four-tuple. When a TCP segment reaches the host, all four fields (source P address, source port, destination P address, destination port) are used to direct (decompose) the segment to the corresponding socket .

Insert picture description here

Web server and TCP

Still look at the picture above.

The figure above shows a server generating a new process for each connection. As shown in the figure, each such process has its own connection socket, through which you can receive HTTP requests and send HTTP responses

  • There is not always a one-to-one correspondence between connection sockets and processes.
  • Today's high-performance Web servers usually use only one process, but create a new thread with a new connection socket for each new client connection. (Thread can be seen as a lightweight child process.)
  • If the client and the server use continuous HTTP, the client and server exchange HTTP messages via the same server socket during the entire duration of the connection. However, if the client and the server use a non-persistent connection, a new TCP connection is created for each request / response pair and then closed, so a new socket is created for each request / response pair and then closed. . The frequent creation and closure of such sockets can severely affect the performance of a busy Web server (although there are many operating system tricks available to mitigate the impact of this problem).

I have seen here, my brothers, sisters, uncles and aunts give Xiao Wang a comment and leave a comment, grow up with Xiao Wang, your attention is my greatest support.
If it's okay, let's take a look: Xiaowang blog directory index, come in and see, maybe you have what you want


If there are any inaccuracies or omissions in the above content, or if you have a better opinion, please leave a comment below to let me know-I will do my best to answer.

Published 23 original articles · praised 266 · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45761327/article/details/105549455