getnameinfo原型要求sockaddr,但我只看到使用sockaddr_in的例子.这个例子可以为sockaddr重写吗? sin_family成为sa_family但是sin_port和sin_addr怎么样?它们如何包含在sa_data中? struct sockaddr{ unsigned s
struct sockaddr{ unsigned short sa_family; char sa_data[14]; };struct sockaddr_in{ short sin_family; unsigned short sin_port; struct in_addr sin_addr; char sin_zero[8]; };
struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(IPvar); sin.sin_port = 0; // If 0, port is chosen by system getnameinfo( (struct sockaddr *)&sin, sizeof(sin), buffervar, sizeof(buffervar), NULL, 0, 0);struct sockaddr是具体协议地址结构的“超类”,如struct sockaddr_in,struct sockaddr_in6和struct sockaddr_un等.getnameinfo(3)根据地址族(sa_family成员)调度到特定的执行路径.
就内存而言 – struct sockaddr_in的三个成员与struct sockaddr的sa_data成员重叠.看一下UnP book的第3章.