我已经知道一个结构的指针可以转换为另一个具有相同内存布局的结构的指针。
但是,在下面的代码中:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT "3490" // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
int main(void)
{
struct sockaddr_storage their_addr;
socklen_t addr_size;
struct addrinfo hints, *res;
int sockfd, new_fd;
// !! don't forget your error checking for these calls !!
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo(NULL, MYPORT, &hints, &res);
// make a socket, bind it, and listen on it:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
listen(sockfd, BACKLOG);
// now accept an incoming connection:
addr_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
// ready to communicate on socket descriptor new_fd!
.
.
.将struct sockaddr_storage their_addr转换为(struct sockaddr *)&their_addr。每个结构的布局是:
struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
}; 和:
struct sockaddr_storage {
sa_family_t ss_family; // address family
// all this is padding, implementation specific, ignore it:
char __ss_pad1[_SS_PAD1SIZE];
int64_t __ss_align;
char __ss_pad2[_SS_PAD2SIZE];
};socktaddr_storage绝对是一个更大的信息存储空间,所以它可以转换为任何比它更小的存储空间,以适应函数声明。在函数内部,什么类型的结构并不重要,只要传入函数的结构有足够的内存块供函数作为所需的结构操作即可。这是正确的吗?
发布于 2011-12-05 17:51:59
是的,它是正确的。基本上,它类似于将派生类指针转换为基类指针(事实上,这就是它在C中实现的方式)。
这14个字节的内容将根据ss_family的值进行设置,然后您可以将其转换为one of the specific structs。
https://stackoverflow.com/questions/8383425
复制相似问题