当前位置:网站首页>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
边栏推荐
猜你喜欢
YOLOv3&YOLOv5输出结果说明
What is erdma? Popular science cartoon illustration
Analysis of CPU surge in production environment service
C crystal report printing
[apipost] tutorial
每日学习3
Xilinx Vivado set *.svh as SystemVerilog Header
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
fatal: unsafe repository is owned by someone else 的解决方法
QT new project
随机推荐
jmeter脚本参数化
什么是 eRDMA?丨科普漫画图解
freemarker的使用
Solving the longest subsequence with linear DP -- three questions
Federated Search: all requirements in search
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
Go operation redis
Design and implementation of car query system based on php+mysql
微信小程序使用towxml显示公式
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
实现一个多进程并发的服务器
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
STM32 library function for GPIO initialization
Makefile 分隔文件名与后缀
[to be continued] [UE4 notes] l5ue4 model import
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
OpenHarmony笔记-----------(四)
求轮廓最大内接圆
TeamTalk源码分析之win-client