当前位置:网站首页>LwIP learning socket (API)
LwIP learning socket (API)
2022-07-03 07:44:00 【Seven demons 71】
Socket Interface programming
Socket API
1.Socket()
Interface prototype
int
lwip_socket(int domain, int type, int protocol)
Function function :
Used according to the specified address family 、 Data type and protocol to allocate the description word of a socket interface and the resources it uses
Function into the reference :
domain Address family AF_INET(IP_V4)、AF_INET6(IP_V6)type Three protocol types are supported Socket protocol types (TCP/UDP/RAW)
#define SOCK_STREAM 1 // Streaming sockets provide reliable 、 Connection oriented traffic ; It USES TCP agreement , So as to ensure the correctness and order of data transmission .
#define SOCK_DGRAM 2// Datagram sockets define a connectionless service , Data is transmitted through independent messages , Is chaotic , And it's not guaranteed to be reliable 、 Error free . It uses datagram protocol UDP.
#define SOCK_RAW 3// Raw sockets allow for underlying protocols such as IP or ICMP Make a direct visit , Powerful but inconvenient to use , Mainly used for the development of some protocols .
protocol
The protocol used by the system , Its definition is as follows
#define IPPROTO_IP 0
#define IPPROTO_ICMP 1
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
Can be protocol The value of the set 0, The system will automatically deduce what protocol should be used . So it is generally written 0.
Return value
Create error return -1
Index number returned after creation
2.blind()
Interface prototype blind
int
lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
Function function :
take sockaddr Some properties described in the structure (IP Address 、 Port number 、 Address cluster ) And socket Socket binding , Also called naming sockets .
Function into the reference :
int s :socket() Returns the socket represented by the file descriptor const struct sockaddr *name: It stores the address and port used by the server for communication .
sockaddr The structure is as follows :
struct sockaddr {
u8_t sa_len;
u8_t sa_family;
char sa_data[14];
};
The actual structure of this parameter depends on the network protocol family , Then add the forced conversion to sockaddr Type in API function . such as AF_INET Will use sockaddr_in Structure as actual structure .
AF_INET6 Will use sockaddr_in6 Structure as actual structure . And then force it to const struct sockaddr *name Solve the warning and error on compilation .
socklen_t namelen
The length of the previous parameter structure .
Return value
Successfully returns 0;
Error return -1; The cause of the error is saved in errno in .( Generally, the address binding is wrong 、 Port occupied )
3.listen()
Interface prototype
int
lwip_listen(int s, int backlog)
Function function :
listen Function uses the active socket interface to be connected socket interface , Make one process accept requests from other processes , And become a server process .
Function into the reference :
int s :socket() Returns the socket represented by the file descriptor backlog: The length of the listening queue , When the number of connections > This value , The requested connection of the client will be rejected
Return value
Successfully returns 0;
Error return -1;
4.accept()
Interface prototype
int
lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
Function function :
Extract the first connection request in the waiting connection queue of the listening socket , Create a new socket , And returns the file descriptor pointing to the socket . It is generally used to block processes in the server , Wait for the client to connect .
Function into the reference :
With the above blind() The input parameters of are consistent and will not be repeated here .
Return value
Returns the file descriptor pointing to the socket .
Failure to return :-1; The cause of the error is saved in errno in .
5.connect()
Interface prototype
int
lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
Function function :
Used to establish and specify socket The connection of . Generally used for client ,
Function into the reference :
With the above blind() The input parameters of are consistent and will not be repeated here .
Return value
Successfully returns :0
Failure to return :-1; The cause of the error is saved in errno in .
6. read / Write read()/write()、recv()/send()
Here is lwip in read() and write() Function source code , Just for recvfrom() and send() Subpackaging read() Function definition :
int
lwip_read(int s, void *mem, size_t len)
{
return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
}
write() Function definition :
int
lwip_write(int s, const void *data, size_t size)
{
return lwip_send(s, data, size, 0);
}
recv() Function definition :
int
lwip_recv(int s, void *mem, size_t len, int flags)
{
return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
}
send() Function definition :
int
lwip_send(int s, const void *data, size_t size, int flags)
Function into the reference :
int s: The socket represented by the target connection file descriptor const void *data: Pointer to send data void *mem: Memory address to receive data int flags: Sign a
7.close()
Interface prototype
int
lwip_close(int s)
Function function :
close socket Connect
Function into the reference :
int s: The socket represented by the file descriptor
Return value
Successfully returns :0
Failure to return :-1
First, let's briefly introduce the common socket Of api function , also select()、lwip_shutdown() Then fill in . After that, we will analyze each api Source code . Deepen the understanding . If there is something wrong in the article, please point it out
边栏推荐
- 技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
- PAT甲级 1029 Median
- Pat grade a 1029 median
- 【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
- The babbage industrial policy forum
- Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
- Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
- Iterm2设置
- Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
- 输入三次猜一个数字
猜你喜欢

Research shows that breast cancer cells are more likely to enter the blood when patients sleep

UA camouflage, get and post in requests carry parameters to obtain JSON format content

OSPF experiment
![[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18](/img/9b/db5fe1a37e0de5ba363f9e108310a5.png)
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18

EtherCAT state machine transition (ESM)

Pat class a 1031 Hello world for u

项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现

技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型

【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机

項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
随机推荐
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Unified handling and interception of exception exceptions of vertx
Inverted chain disk storage in Lucene (pfordelta)
The concept of C language pointer
JUnit unit test of vertx
PAT甲级 1029 Median
Go language foundation ----- 11 ----- regular expression
Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
Go language foundation ------ 12 ------ JSON
List exercises after class
Web router of vertx
Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
PHP常用排序算法
技术干货|昇思MindSpore创新模型EPP-MVSNet-高精高效的三维重建
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
华为交换机Console密码重置、设备初始化、默认密码
C2 several methods of merging VCF files
GoLang之结构体
Industrial resilience
Lucene merge document order