The understanding of Socket can be roughly divided into the following topics, what is Socket, how is Socket created, how Socket is connected and sent and received data, and Socket is deleted.
What is Socket and its creation process
A data packet is generated by the application program, enters the protocol stack to package various message headers, and then the operating system calls the network card driver to instruct the hardware to send the data to the opposite host. The general diagram of the whole process is as follows.
As we all know, the protocol stack is actually a stack of some protocols in the operating system. These protocols include TCP, UDP, ARP, ICMP, IP etc. Usually a certain protocol is designed to solve certain problems. For example, the design of TCP is responsible for the safe and reliable transmission of data. The design of UDP is to have small packets and high transmission efficiency. ARP is designed to be able to query the physical (Mac) by IP address. Address, the design purpose of ICMP is to return error messages to the host, and the purpose of IP design is to realize the interconnection of large-scale hosts.
Data generated by applications such as browsers, e-mails, file transfer servers, etc. will be transmitted through the transport layer protocol, and applications will not directly establish contact with the transport layer, but have a connection between the application layer and the transport layer The kit in between, this kit is Socket
.
In the above picture, the application includes a Socket and a resolver. The role of the resolver is to initiate a query to the DNS server to query the target IP address.
Below the application program is the inside of the operating system. The inside of the operating system includes a protocol stack, which is a stack of a series of protocols. Below the operating system is the network card driver, the network card driver is responsible for controlling the network card hardware, and the driver drives the network card hardware to complete the sending and receiving work.
Inside the operating system, there is a storage space for storing control information, and this storage space records the control information used for control communication. In fact, the control information is the entity of the Socket, or the memory space storing the control information is the entity of the socket .
You may not know why here, so I used the netstat command to show everyone what sockets are.
We enter in the Windows command prompt
netstat -ano
# netstat 用于显示套接字内容 , -ano 是可选选项
# a 不仅显示正在通信的套接字,还显示包括尚未开始通信等状态的所有套接字
# n 显示 IP 地址和端口号
# o 显示套接字的程序 PID
The following result will appear on my computer.
Each row in the figure is equivalent to a socket, and each column is also called a tuple, so a socket is a five-tuple (protocol, local address, external address, status, PID). Sometimes it is also called a quadruple, which does not include the protocol.
For example, in the first line of the figure, its protocol is TCP, the local address and remote address are both 0.0.0.0, which means that the communication has not yet started, the IP address has not yet been determined, and the local port is known to be 135, but the remote port also unknown at this time the state is LISTENING
, LISTENING indicates that the application is already open, is waiting to establish a connection with the remote host (on transitions between various states, you can read the author of this article TCP, Hell has finally come ) The last tuple is PID, which is the process identifier. PID is like our ID number, which can accurately locate the unique process.
Now you may have a basic understanding of Socket, now take a drink and take a break, let us continue to explore Socket.
Now I have a question, how is the socket created?
Socket is created together with the application. There is a socket component in the application. When the application starts, it will call the socket to apply for creating a socket. The protocol stack will create the socket according to the application's application: first allocate the memory space required by a socket, this step It is equivalent to preparing a container for control information, but only the container has no practical effect, so you also need to put control information into the container; if you don’t apply for the memory space needed to create a socket, the control information you create will also not Place storage, so allocates memory space, and puts control information in it. At this point, the creation of the socket has been completed.
After the socket is created, it will return a socket descriptor to the application. This descriptor is equivalent to the number plate that distinguishes different sockets. According to this descriptor, the application needs to provide this descriptor when entrusting the protocol stack to send and receive data.
Socket connection
After the socket is created, it will finally serve the data transmission and reception. Before the data transmission and reception, a step connect
is needed, which is the process of establishing a connection. This connection is not a real connection: a water pipe is inserted between the two computers.
It is a process in which the application program is transmitted from one host to another host through the network medium through the TCP/IP protocol standard.
Just after the socket is created, there is no data yet, and no communication partner is known. In this state, even if you let the client application delegate the protocol stack to send data, it doesn't know where to send it. Therefore, the browser needs to query the server's IP address based on the URL. The protocol for doing this work is DNS. After the target host is queried, it then informs the protocol stack of the target host's IP. At this point, the client is ready.
On the server, like the client, it also needs to create a socket, but it also does not know who it is communicating with, so we need to let the client inform the server of the necessary information of the client: IP address and port number .
Now that the necessary information for the communication parties to establish a connection is available, only one shareholder is left. After the communication parties receive the data, they need a location to store it. This location is the buffer, which is part of the memory. With the buffer, the data can be sent and received.
OK, now the client wants to send a piece of data to the server, what should it do?
First, the client application needs to call Socket
library to provide the socket descriptor and server IP address and port number.
connect(<描述符>、<服务器IP地址和端口号>)
This information will be passed to the TCP module in the protocol stack. The TCP module will encapsulate the request message, and then pass it to the IP module to encapsulate the IP header, then pass it to the physical layer for frame header encapsulation, and then pass through the network The media is passed to the server, and the server will parse the headers of the frame header, IP module, and TCP module to find the corresponding socket. After the socket receives the request, it will write the corresponding information and change the status. Connecting now. After the request process is completed, the server's TCP module will return a response. This process is the same as the client. (If you are not sure about the encapsulation process of the message header, you can read this author's article TCP/IP basic knowledge summary )
In a complete request and response process, control information plays a very critical role (we will talk about the specific role later).
- SYN is the abbreviation for synchronization. The client will first send a SYN packet to request the server to establish a connection.
- ACK is the corresponding meaning, and it is a response to sending a SYN packet.
- FIN means termination, which means that the client/server wants to terminate the connection.
Due to the complex and changeable network environment, data packets are often lost. Therefore, both parties need to confirm whether the other party's data packets have arrived when communicating, and the criterion for judgment is the value of ACK.
(The establishment of the connection between the two communication parties will go through a three-way handshake process. For a detailed introduction to the three-way handshake, you can read this author's article TCP basics )
When all the connected messages can be sent and received normally, the socket has entered the state of sending and receiving at this time. At this time, it can be considered that the two sockets are connected with one management. Of course, this tube does not actually exist. After the connection is established, the connection operation of the protocol stack is over, that is to say, the connect has been executed and the control flow is handed back to the application.
Send and receive data
When the control flow returns from connect to the application program, it will directly enter the data receiving and sending stage. The data receiving and sending operation starts from the application calling write and handing over the data to be sent to the protocol stack, and the protocol stack executes the sending operation after receiving the data. .
The protocol stack does not care what data is transmitted by the application, because these data will eventually be converted into a binary sequence. The protocol stack will not send the data immediately after receiving the data, but will put the data in the sending buffer Area , and then wait for the application to send the next piece of data.
Why is the received data packet not sent directly, but placed in the buffer?
Because as long as the data is received, it will be sent, it is possible to send a large number of small data packets, resulting in a decrease in network efficiency. So the protocol stack needs to accumulate data to a certain amount before sending it out. As for how much data the protocol stack will put into the buffer, different versions and types of operating systems have different opinions. However, all operating systems and types will follow the following standards:
- The first judgment element is the data length that each network packet can hold. The judgment standard is
MTU
, which represents the maximum length of a network packet. The maximum length includes the header, so if you discuss the data area alone, MTU-header length will be used. The resulting maximum data length is calledMSS
.
- Another criterion is time. When the data generated by the application program is relatively small and the efficiency of the protocol stack to place the data in the buffer is not high, if you wait until the MSS is sent each time, the delay may be caused by the long waiting time. In this case, even if the data length does not reach the MSS, the data should be sent out.
The protocol stack does not tell us how to balance these two factors. If the length of the data is prioritized, then the efficiency may be lower; if the time is prioritized, it will reduce the efficiency of the network.
After a while. . . . . .
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gxdjgvl9iqg30m80godky.gif" alt="img" style="zoom: 25%;" />
Suppose we are using the law of finite length. At this time, the buffer is full and the protocol stack is about to send data. The protocol stack is about to send the data, but found that it is impossible to transmit such a large amount of data (relatively) at one time. How to do it?
In this case, the data in the sending buffer will exceed the length of MSS, and the data in the sending buffer will be split with the size of MSS as a packet. Each piece of split data will be added with TCP, IP , The Ethernet header is then put into a separate network packet.
Up to now, the network packet is ready to be sent to the server, but the data sending operation has not ended, because the server has not yet confirmed whether it has received the network packet. Therefore, after the client sends the data packet, the server also needs to confirm.
When the TCP module splits the data, it calculates the network packet offset. This offset is the number of bytes calculated from the beginning of the data, and the calculated number of bytes is written in the TCP header. The TCP module A network packet serial number (SYN) is also generated. This serial number is unique, and this serial number is used for confirmation by the server.
The server will confirm the data packet sent by the client. After the confirmation is correct, the server will generate a sequence number and an acknowledgment number (ACK) and send it to the client together. The client will send the acknowledgment number to the server after confirming.
Let's take a look at the actual working process.
First, the client needs to calculate the initial value of the serial number when connecting, and send this value to the server. Next, the server calculates the confirmation number from this initial value and returns it to the client. The initial value may be discarded during the communication process, so when the server receives the initial value, it needs to return a confirmation number for confirmation. At the same time, the server also needs to calculate the initial value of the sequence number from the server to the client, and send this value to the client. Then, the client also needs to calculate the confirmation number according to the initial value sent by the server and send it to the server. At this point, the connection is established, and then the data sending and receiving stage can be entered.
In the data receiving and sending stage, both parties in communication can send requests and responses at the same time, and both parties can confirm the request at the same time.
The request-confirmation mechanism is very powerful. Through this mechanism, we can confirm whether the receiver has received a certain packet and resend it if it has not been received. In this way, we can even find any errors in the network. And remedy.
network card, hub, and router have no error remediation mechanism. Once an error is detected, the data packet will be directly discarded, and the application does not have this mechanism. It is only the TCP/IP module that works.
Due to the complex and changeable network environment, data packets will be lost. Therefore, there are certain rules for sending sequence numbers and confirmation numbers. TCP will manage the confirmation numbers through the window. We will not repeat them in this article. You can read this article by the author. TCP basics to find the answer.
Disconnect
When the communication parties no longer need to send and receive data, they need to disconnect. Different applications have different timings for disconnection. Taking the Web as an example, the browser sends a request message to the Web server, and the Web server returns a response message. At this time, the sending and receiving of data is all over. The server may initiate a disconnection response first, and of course the client may initiate it first (who first Disconnection is a judgment made by the application program) and has nothing to do with the protocol stack.
No matter which party initiates the disconnect request, the close program of the Socket library will be called. Let’s take the server disconnect as an example. The server initiates a disconnect request, and the protocol stack generates a disconnected TCP header. In fact, it sets the FIN bit and then entrusts the IP module to send data to the client. At the same time, the server The socket will record the disconnection related information .
After receiving the FIN request from the server, the client protocol stack will mark the socket as disconnected, and then the client will return a confirmation number to the server. This is the first step to disconnect. In this step After that, the application will also call read to read the data. After the server data is sent, the protocol stack will notify the client that the data has been received by the application program.
As long as it receives all the data returned by the server, the client will call the close program to end the sending and receiving operations. At this time, the client will generate a FIN and send it to the server. After a period of time, the server will return an ACK number. At this point, the communication between the client and the server is complete. it's over.
Delete socket
After the communication is completed, the socket used for communication will no longer be used, and we can delete the socket at this time. However, the socket will not be deleted immediately at this time, but will be deleted after a period of time.
Waiting for this period of time is to prevent misoperation. The most common misoperation is the loss of the confirmation number returned by the client. As for how long to wait, it is related to the way the data packet is retransmitted.
Original link: this is Socket!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。