头文件 : netinet/in.h
IPv4:
struct in_addr { in_addr_t s_addr; //uint32 };
struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; //地址族 in_port_t sin_port; //Port uint16 struct in_addr sin_addr; //IP unsigned char sin_sero[8]; //填充‘0’, };
struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; }sa_data = sin_port + sin_addr + sin_zero
字符串转in_addr的函数:
#include <arpa/inet.h> int inet_aton(const char *strptr, struct in_addr *addrptr); in_addr_t inet_addr(const char *strptr); int inet_pton(int family, const char *strptr, void *addrptr);
in_addr转字符串的函数:
char *inet_ntoa(struct in_addr inaddr); const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);
domain : AF_INET, AFINET6
addr: 2进制IP
str : xxx.xxx.xxx.xxx
size: 3*4 + 3*1 + '0' = 16
void bzero( void *dest, size_t nbytes );
void bcopy( const void *src, void *dest, size_t nbytes );
void ( const void *ptr , const void *ptr2, size_t nbytes );
[1] Linux 环境下C编程指南