当前位置:网站首页>What is socket? Basic introduction to socket

What is socket? Basic introduction to socket

2022-07-05 06:18:00 Ostrich5yw

One 、 What is socket ?

Socket is a communication mechanism ( An agreement between two parties to a communication ),socket Shielding the communication details of each protocol , Provides tcp/ip Protocol abstraction , It provides a set of external interfaces , With this interface, it can be unified 、 Easy to use tcp/ip Functions of the protocol . This allows programmers to ignore the protocol itself , Use it directly socket The interface is provided for the process communication between different interconnected hosts . We can use the related functions in the socket to complete the communication process .
 Insert picture description here
The processing flow of sending data of the sender is roughly as follows : User space -> kernel -> network card -> The Internet

In user state space , Call send data interface send/sento/wirte Wait for the packet to be written , In the kernel space, different processes will be followed according to different protocols . With TCP For example ,TCP It's a streaming protocol , The kernel just appends packets to the socket's send queue , When the data is actually sent , It is from TCP Protocol to control .TCP After the agreement is processed, it will be handed over to IP The agreement continues to deal with , Finally, the sending function of the network card will be called , Send the packet to the network card .

The process flow of receiving data of the receiver is roughly as follows : The Internet -> network card -> kernel (epoll etc. ) -> process ( Business processing logic )

The network card will receive data by polling or notification ,Linux Optimized , A combination of notification and polling mechanisms , Simply speaking , stay CPU When responding to network card interrupt , It's no longer just processing a packet and exiting , Instead, it uses polling to continue trying to process new packets , Until no new packets arrive , Or reach the set maximum number of packets to be processed at one interrupt . After leaving the network card driver, the data enters the protocol stack , after IP layer 、 Network layer protocol processing , It will trigger IO Read events , such as epoll Of reactor In the model , The corresponding read event will be triggered , Then call back the corresponding IO Processing function , The data is then handed over to the business thread for processing , such as Netty The data receiving and processing flow is like this .

Two 、 Socket features

There are three properties that determine the properties of a socket , They are : Domain (domain), type (type), And the protocol (protocol).
Domain : Specify the network media used in socket communication . The most common socket domain is AF_INET(IPv4) perhaps AF_INET6(IPV6), It means Internet The Internet .
type :

  • Stream Socket (SOCK_STREAM):
    Stream sockets are used to provide connection oriented 、 Reliable data transfer service . The service will ensure that the data can be error free 、 No duplicate transmission , And receive... In order . The reason why streaming socket can realize reliable data service , The reason is that it uses transmission control protocol , namely TCP
  • Socket datagram (SOCK_DGRAM):
    Datagram socket provides a connectionless service . This service does not guarantee the reliability of data transmission , Data may be lost or duplicated during transmission , And there is no guarantee that data will be received in sequence . Datagram socket use UDP(User Datagram Protocol) Protocol for data transmission .
  • Raw socket (SOCK_RAW):
    Raw socket and standard socket ( Standard socket refers to the streaming socket and datagram socket described earlier ) The difference is that : The original socket can read and write what the kernel doesn't handle IP Data packets , The streaming socket can only read TCP Protocol data , Datagram socket can only read UDP Protocol data . therefore , If you want to access other protocols to send data, you must use the original socket .

agreement :IPPROTO_TCP,IPPROTO_UDP

3、 ... and 、 Socket buffer

Every socket After being created , Both buffers are allocated , Input buffer and output buffer .write()/send() Do not transfer data to the network immediately , Instead, write the data to the buffer first , Again by TCP The protocol sends data from the buffer to the target machine . Once the data is written to the buffer , Function to return , Whether they reach the target machine or not , And whenever they're sent to the network , These are all TCP What the agreement is responsible for .read()/recv() The same is true of functions , Also read data from the input buffer , Instead of reading directly from the network .
 Insert picture description here
User program buffer
When a user process accesses system resources through a system call , You need to switch to kernel mode , This corresponds to some special stack and memory environment , Must be established before the system call . And after the system call ,cpu Will switch back from core mode to user mode , The stack must be restored to the context of the user process . And this switching will take a lot of time .
Some programs read files , Will first apply for a memory array , be called buffer, And then every time you call read, Read the data with the set byte length , write in buffer( Fill... With a small number of times buffer). The subsequent procedures are from buffer Get data in , When buffer After use , Before making the next call , fill buffer. So : The purpose of user buffer is to reduce the number of system calls , So as to reduce the time spent by the operating system switching between user state and core state . In addition to designing buffers in the process , The kernel also has its own buffer .
Kernel buffer
When a user process wants to read data from disk , The kernel generally does not read the disk directly , Instead, the data in the kernel buffer is copied to the process buffer . But if there is no data in the kernel buffer , The kernel will send requests for data blocks , Join the request queue , Then suspend the process , Serve other processes . Wait until the data has been read into the kernel buffer , Read the data in the kernel buffer into the user process , Will notify the process .
You can think ,read Is to copy data from the kernel buffer to the process buffer .write Is to copy the process buffer to the kernel buffer . Of course ,write It doesn't necessarily lead to the write action of the kernel , such as os The kernel buffer data may be accumulated to a certain amount , Write... Again . That's why power outages sometimes lead to data loss . So the kernel buffer , In order to in OS Level , Improve the disk IO efficiency , Optimize disk write operations .

原网站

版权声明
本文为[Ostrich5yw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050615250681.html