1、介绍sockaddr
结构体struct sockaddr在/usr/include/linux/socket.h中定义
struct sockaddr {unsigned short sa_family; // 2 bytes address family, AF_xxxchar sa_data[14]; // 14 bytes of protocol address
};
2、介绍sockaddr_in
结构体struct sockaddr_in在/usr/include/netinet/in.h中定义,是IPv4的地址结构
struct sockaddr_in {short sin_family; // 2 bytes e.g. AF_INET, AF_INET6unsigned short sin_port; // 2 bytes e.g. htons(3490)struct in_addr sin_addr; // 4 bytes see struct in_addr, belowchar sin_zero[8]; // 8 bytes zero this if you want to
};struct in_addr {unsigned long s_addr; // 4 bytes load with inet_pton()
};
3、对比总结
struct sockaddr是通用的套接字地址,而struct sock