当前位置:网站首页>What is socket? Basic introduction to socket
What is socket? Basic introduction to socket
2022-07-05 06:18:00 【Ostrich5yw】
What is socket ?Socket Basic introduction
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 .
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 TCPSocket 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 .
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 .
边栏推荐
- Leetcode heap correlation
- 【Rust 笔记】14-集合(上)
- SQL三种连接:内连接、外连接、交叉连接
- Spark中groupByKey() 和 reduceByKey() 和combineByKey()
- Presentation of attribute value of an item
- Quickly use Amazon memorydb and build your own redis memory database
- MySQL advanced part 1: index
- 阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
- Leetcode-22: bracket generation
- Redis publish subscribe command line implementation
猜你喜欢
开源存储这么香,为何我们还要坚持自研?
Arduino 控制的 RGB LED 无限镜
MIT-6874-Deep Learning in the Life Sciences Week 7
Redis publish subscribe command line implementation
WordPress switches the page, and the domain name changes back to the IP address
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
liunx启动redis
【LeetCode】Easy | 20. Valid parentheses
数据可视化图表总结(二)
随机推荐
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
[leetcode] day95 effective Sudoku & matrix zeroing
什么是套接字?Socket基本介绍
How to generate an image from text on fly at runtime
Operator priority, one catch, no doubt
Nested method, calculation attribute is not applicable, use methods
redis发布订阅命令行实现
LVS简介【暂未完成(半成品)】
leetcode-1200:最小绝对差
Traversal of leetcode tree
Leetcode-6111: spiral matrix IV
MySQL advanced part 1: triggers
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
[rust notes] 17 concurrent (Part 2)
1.14 - assembly line
SQL三种连接:内连接、外连接、交叉连接
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
One question per day 1020 Number of enclaves