一、套接字地址结构
说套接字编程,第一个就是套接字地址结构。通用的套接字地址结构定义如下:
#include <sys/socket.h> struct sockaddr{ uint8_t sa_len; sa_family_t sa_family; // address family : AF_XXX char sa_data[14]; }; |
最常用的sockaddr_xxx就是IPv4套接字地址结构sockaddr_in:
#include <netinet/in.h> struct in_addr{ in_addr_t s_addr; // 32-bit IPv4 address, network byte ordered }; struct sockaddr_in{ uint8_t sin_len; // length of structure sa_family_t sin_family; // AF_INET in_port_t sin_port; // 16-bit TCP or UDP port number, network byte ordered struct in_addr sin_addr; // 32-bit IPv4 address, network byte ordered char sin_zero[8]; }; |