当前位置:网站首页>Socket and socket address
Socket and socket address
2022-07-02 14:37:00 【Kallou】
To understand socket, First of all, understand TCP/IP Four layer model .
1.TCP/IP Four layer model
TCP/IP The four layer model is right OSI Simplification of the seven layer model , It consists of four layers from top to bottom , Namely application layer 、 Transport layer 、 The network layer 、 The link layer . Each layer has several rules to ensure its interaction with the upper and lower levels , That is to say agreement , The two parties who communicate must abide by the agreement in order to be successful 、 Send and receive data efficiently . In these agreements , On the transport layer TCP Protocol and at the network layer IP The protocol is the most frequently used , The most widely applicable agreement , therefore TCP/IP The protocols in the four tier model are collectively referred to as TCP/IP Protocol family .

2. What is? socket?
socket It's located in The intermediary between the application layer and the transport layer , Up ,socket It is responsible for receiving the data that the application layer process will send through the network and transmitting the data sent by the network to the process ; Down ,socket It is responsible for receiving the data sent by the transmission layer and the data sent by the sending process using the network .socket It can be regarded as both ends of network communication Interface , stay Linux in , Its The essence is a kind of document , Relevant information of the end is recorded . in other words , There will be no direct face-to-face communication between the application layer and the transport layer , But through socket With A unified 、 Safe Network data transmission .
Why? Linux Medium socket It is regarded as a file type ?
stay Linux There are all files in File descriptor ,socket The advantage of being considered a document is Linux The system can use file descriptors , Operate like a general file socket, This keeps Linux Operation documents and socket Of Unity .
3.socket What kind ?
socket There are many types of sockets , What we often use is DARPA Internet Socket . According to the data transmission mode ,Internet There are also many types of sockets , There are two kinds of common , namely Streaming socket (SOCK_STREAM) and Datagram format socket (SOCK_DGRAM)
Streaming socket (SOCK_STREAM):
Streaming socket The bottom layer follows TCP agreement , Therefore, it has TCP Many features of the agreement , namely No loss , No disorder , Asynchronous sending and receiving ( That is, the sending and receiving of data are not synchronized ,TCP Chinese data may arrive in batches , You can wait for all the data to arrive before unified reception , This is due to TCP Set the buffer to ensure )
stay http What is used in the protocol is stream format socket , because html The file must be loaded accurately .
Datagram format socket (SOCK_DGRAM):
Datagram format socket The bottom layer follows UDP agreement , Therefore, it has UDP The features of the agreement , namely Can be lost or damaged , Limit the size of single transmission data , Send and receive synchronously ( contrast TCP Asynchronous sending and receiving ,UDP The data required to be delivered in must be received immediately , Whether or not it is part of a complete data )
4.socket Address
Network communication The essence is interprocess communication , Therefore, in the process of network communication, both parties must know each other's Process port number and IP Etc ,socket Address Is a kind of record of this information data structure .
socket The address in socket Network programming API It is called sockaddr The structure of the body , There are two members :
1.sa_family Represents the address family of both sides of the communication
2.sa_data Express socket Address values . According to the address family , The way to interpret the address value is also different .

Common address family and its corresponding protocol family :

macro PF_* and AF_* It's all defined in bits/socket.h Header file , And the latter has exactly the same value as the former , therefore The two are usually mixed .
How to interpret the address value corresponding to the protocol family :

It can be known from the above table ,14 Bytes of sa_data Can't hold address values for most protocol families at all . therefore ,Linux Defines a new generic socket Address structure -sockaddr_storage, This structure not only provides enough space for storing address values , And it's memory aligned :

meanwhile , For the convenience of sockaddr and sockaddr_storage assignment , Defined special socket Address structure .
5. special socket Address and general socket Address
Due to the development of network communication technology , Once the sockaddr Not compatible Data under many modern agreements , For example, in the above IPv4 Protocol family of , If you use sockaddr It is quite troublesome for a structure to assign its address value ( because sa_data It's a whole continuous space , We need to put data with different meanings , Then set 0-1byte Put on the slogan ,2-5byte discharge IP Address ), and IPv6 The address value of the protocol family cannot even be used sockaddr Install .
So now sockaddr and sockaddr_storage Are regarded as a kind of General purpose socket Address , And a lot of new definitions are dedicated to various protocols special socket Structure , When calling related functions, special socket Address cast to generic socket Address type .
say concretely , With socket API Medium bind() Function as an example ,
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
The second parameter needs to be specified socket Address , The type of parameter is struct sockaddr*, And if you use IPv4 agreement , The actual parameter data type that can be passed is struct sockaddr_in Structure , And cast :
struct sockaddr_in sinaddr;
bind(...,(struct sockaddr*)&sinaddr,...);
Reference resources :
socket What is it? ? What is socket ?
Sign in — major IT Written interview preparation platform _ Cattle from
边栏推荐
- Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
- PyQt5_ Qscrollarea content is saved as a picture
- Data Lake (11): Iceberg table data organization and query
- STM32 standard firmware library function name (I)
- mongodb的认识
- 检查密码
- Design and implementation of car query system based on php+mysql
- Thymeleaf dependency
- Basic knowledge of QT original code
- Adhere to the foundation of 20 minutes go every day II
猜你喜欢

Quick analysis: easy to share the Internet

Pychart connects to the remote server

报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

Makefile 分隔文件名与后缀

< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621

obsidian安装第三方插件——无法加载插件

Analysis of CPU surge in production environment service

每日学习3

A white hole formed by antineutrons produced by particle accelerators

jmeter脚本参数化
随机推荐
buuctf-pwn write-ups (7)
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
NLA natural language analysis realizes zero threshold of data analysis
Methods of software testing
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Yolov6 training: various problems encountered in training your dataset
Using computed in uni app solves the abnormal display of data () value in tab switching
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
跨服务器数据访问的创建链接服务器方法
PyQt5_ Qscrollarea content is saved as a picture
Fabric. JS free drawing ellipse
Data Lake (11): Iceberg table data organization and query
Xilinx Vivado set *. svh as SystemVerilog Header
C语言高级用法--函数指针:回调函数;转换表
Chapter 9: xshell free version installation
C crystal report printing
天猫商品详情接口(APP,H5端)
fatal: unsafe repository is owned by someone else 的解决方法