导航

2021年4月3日 #

socket三种地址结构

摘要: /* <sys/socket.h> */ struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; }; /* <netinet/in.h> */ struct sockaddr_in { uint8_t s 阅读全文

posted @ 2021-04-03 23:19 toughcactus 阅读(106) 评论(0) 推荐(0) 编辑

非阻塞connect

摘要: int flags = fcntl(sock_fd, F_GETFL, 0); fcntl(sock_fd, F_SETFL, flags|O_NONBLOCK); #else int imode = 1; ioctl(sock_fd, FIONBIO, &imode); 阅读全文

posted @ 2021-04-03 22:15 toughcactus 阅读(63) 评论(0) 推荐(0) 编辑

socket字符串,数值处理函数

摘要: inet_pton 字符串转为网络字节序整数,网络字节序可以直接用于sockaddr_in。 #include <sys/socket.h> #include <netinet/in.h> #include<arpa/inet.h> atoi 字符串是ASCII 将字符串转为整数 #include 阅读全文

posted @ 2021-04-03 20:35 toughcactus 阅读(69) 评论(0) 推荐(0) 编辑

valist

摘要: 最近在编写日志模块时,用到va_list,因为输出会包含不定数目的参数。 要求是在函数参数列表里,用...表明多个参数,但...之前必须得有一个参数。 声明va_list args, void logDebug(char *p, ...) { va_list args; /* 之所以...前要有一个 阅读全文

posted @ 2021-04-03 20:04 toughcactus 阅读(218) 评论(0) 推荐(0) 编辑

静态变量和全局变量浅析

摘要: 代码检视的时候,前辈建议全局变量添加g作前缀,静态变量添加s作前缀。 所谓全局变量,即在.c文件函数体外声明,可以在.h文件extern,这样,所有包含.h文件的情况下,均可使用该变量。 静态变量会添加static关键字,在.c文件函数体外声明,不会在.h文件extern。也就是说,只有.c文件内的 阅读全文

posted @ 2021-04-03 17:48 toughcactus 阅读(116) 评论(0) 推荐(0) 编辑