serv_addr.sin_addr = *((struct in_addr *)host->h_addr);请高手详细解释一下这句话?非常感谢!
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/24 03:40:52
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);请高手详细解释一下这句话?非常感谢!
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);请高手详细解释一下这句话?非常感谢!
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);请高手详细解释一下这句话?非常感谢!
struct sockaddr_in {
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // Same size as struct sockaddr
};
struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};
struct in_addr {
unsigned long s_addr; // that’s a 32-bit long, or 4 bytes
};
在填充sockaddr_in结构体时:serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
sockaddr_in与sockaddr都是16个字节,只是sockaddr的表现形式更形象,对象化而已.而in_addr结构体就是一个long型
可以猜测h_addr_list是一个4字节网络字节序的in_addr,只是使用char*来表示,h_addr与struct in_addr其实形式是一样的.然后将host->h_addr强转成struct in_addr格式的指针,最后取值.