C语言 协议不支持的地址族
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20368832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Address family not supported by protocol
提问by user1345414
Following code is a socket programming sample for a TCP client.
以下代码是 TCP 客户端的套接字编程示例。
But when I run this, connect() is returned as Address family not supported by protocol.
但是当我运行它时,connect() 作为协议不支持的地址族返回。
I have heard, this problem will happen if the platform does not support ipv6.
我听说如果平台不支持ipv6就会出现这个问题。
But AF_INET I wrote is ipv4.
但是我写的AF_INET是ipv4。
Also my server, that is CentOS6.4, is configured within an inet6 addr .
还有我的服务器,即 CentOS6.4,配置在 inet6 addr 中。
Does anyone know why?
有谁知道为什么?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int
main(){
struct sockaddr_in server;
int sock;
char buf[32];
int n;
sock = socket(AF_INET,SOCK_STREAM,0);
perror("socket");
server.sin_family = AF_INET;
server.sin_port = htons(12345);
inet_pton(AF_INET,"127.0.0.1",&server,sizeof(server));
connect(sock,(struct sockaddr *)&server,sizeof(server));
perror("connect");
memset(buf,0,sizeof(buf));
n = read(sock,buf,sizeof(buf));
perror("read");
printf("%d,%s\n",n,buf);
close(sock);
return 0;
}
采纳答案by alk
The code passes the wrong destination address and wrong number of arguments to inet_pton(). (For the latter the compiler should have warned you about, btw)
代码将错误的目标地址和错误数量的参数传递给inet_pton(). (对于后者,编译器应该警告你,顺便说一句)
This line
这条线
inet_pton(AF_INET, "127.0.0.1", &server, sizeof(server));
should be
应该
inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
Verbatim from man inet_pton:
逐字来自man inet_pton:
int inet_pton(int af, const char *src, void *dst);
AF_INET
[...] The address is converted to a struct in_addr and copied to dst, which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
int inet_pton(int af, const char *src, void *dst);
AF_INET
[...] 该地址被转换为结构 in_addr 并复制到 dst,其长度必须为 sizeof(struct in_addr) (4) 字节(32 位)。
Not related to the problem, but also an issue, is that read()returns ssize_tnot int.
与问题无关,但还有一个问题,就是read()返回ssize_tnot int。
The following lines shall be adjusted:
应调整以下几行:
int n;
[...]
printf("%d, %s\n", n, buf);
to become:
成为:
ssize_t n;
[...]
printf("%zd, %s\n", n, buf);
回答by sundq
Set the server address like this;
像这样设置服务器地址;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(host);
addr.sin_port = htons(port);
回答by dyomas
I seen this error during bind. Cause was of using localhostinstead of IP:
我在bind期间看到了这个错误。原因是使用localhost而不是 IP:
./myprogram localhost:7777
*** exception! `bind' failed for `localhost:7777' (97, Address family not supported by protocol)
./myprogram 127.0.0.1:7777
OK! Listening...
In addition: this error happens on one Linux host and does not appear on another. I check and compare network settings on this machines (lodevice, /etc/hosts, /etc/host.conf, etc) and not found essential difference
另外:此错误发生在一台 Linux 主机上,而不会出现在另一台主机上。我检查并比较了这台机器上的网络设置(lo设备、/etc/hosts、/etc/host.conf 等),但没有发现本质区别

