gpt4 book ai didi

c - 绑定(bind)失败 : : Address family not supported by protocol family - C

转载 作者:太空宇宙 更新时间:2023-11-04 05:24:56 42 4
gpt4 key购买 nike

我在主函数中声明了一个struct sockaddr_in server

我将它传递给这个函数以返回一个套接字文件描述符

int openSocket(char* ip_addr, int port, struct sockaddr_in* server){

int sockfd, len;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if(sockfd < 0){
perror("Failed to open socket: ");
exit(-1);
}

len = sizeof(server);
bzero(server, len);
server->sin_family= AF_INET;
inet_pton(AF_INET, ip_addr, &server->sin_addr);
server->sin_port= htons(port);

if((bind(sockfd, (struct sockaddr*)&server, len)) < 0){
perror("Bind failed: ");
exit(-1);
}
return sockfd;

}

使用此调用将结构传递给函数

sockfd = openSocket(vector->ip_addr, vector->port, &server);

但是我得到以下错误。

 Bind failed: : Address family not supported by protocol family

最佳答案

  len = sizeof(server);

这决定了指针的大小。你想要它指向的结构的大小,所以这应该是:

  len = sizeof(*server);

关于c - 绑定(bind)失败 : : Address family not supported by protocol family - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838997/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com