linux网络套接字//accept参数为什么不用struct sockaddr结构体?
C/C++ code
struct sockaddr_in fromaddr={};
socklen_t len;
int sd = accept(sockfd, (struct sockaddr*)&fromaddr,&len);
------解决方案--------------------
因为struct sockaddr兼容所有其他地址类型,因为第一个字段都是一样的,那就是:
sa_family_t sa_family;
所以只要解析为sockaddr,获取sa_family字段,然后switch case转化为具体某个类型的地址,比如IPV4,IPV6,UN。
The <sys/socket.h> header shall define the sockaddr structure that includes at least the following members:
sa_family_t sa_family Address family.
char sa_data[] Socket address (variable-length data).