IPv6:connect() 总是失败并显示 errno 22
操作系统是Ubuntu。 我正在对基本 IPv6 操作进行简单测试。 PC通过集线器与IP Camera(支持IPv6)连接。 ping6 测试成功。
$ ping6 -I eth1 fe80::240:8cff:fe94:451e
PING fe80::240:8cff:fe94:451e(fe80::240:8cff:fe94:451e) from fe80::224:8cff:fe90:ad3b eth1: 56 data bytes
64 bytes from fe80::240:8cff:fe94:451e: icmp_seq=1 ttl=64 time=3.86 ms
64 bytes from fe80::240:8cff:fe94:451e: icmp_seq=2 ttl=64 time=0.471 ms
代码如下:
#include <linux/in6.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
void main()
{
int s, ret, err;
struct sockaddr_in6 addr;
s = socket(AF_INET6, SOCK_STREAM, 0);
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(554);
addr.sin6_flowinfo = 0;
addr.sin6_scope_id = 0;
addr.sin6_addr.s6_addr[0] = 0xfe;
addr.sin6_addr.s6_addr[1] = 0x80;
addr.sin6_addr.s6_addr[2] = 0x00;
addr.sin6_addr.s6_addr[3] = 0x00;
addr.sin6_addr.s6_addr[4] = 0x00;
addr.sin6_addr.s6_addr[5] = 0x00;
addr.sin6_addr.s6_addr[6] = 0x00;
addr.sin6_addr.s6_addr[7] = 0x00;
addr.sin6_addr.s6_addr[8] = 0x02;
addr.sin6_addr.s6_addr[9] = 0x40;
addr.sin6_addr.s6_addr[10] = 0x8c;
addr.sin6_addr.s6_addr[11] = 0xff;
addr.sin6_addr.s6_addr[12] = 0xfe;
addr.sin6_addr.s6_addr[13] = 0x94;
addr.sin6_addr.s6_addr[14] = 0x45;
addr.sin6_addr.s6_addr[15] = 0x1e;
ret = connect(s, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1)
{
err = errno;
printf("connect failure, errno = %d\n", err);
}
}
结果始终是“连接失败,errno = 22” 问题出在哪里?
The OS is Ubuntu.
I'm doing a simple test for basic IPv6 operations.
The PC is connected with an IP Camera (support IPv6) via a hub.
ping6 testing is successful.
$ ping6 -I eth1 fe80::240:8cff:fe94:451e
PING fe80::240:8cff:fe94:451e(fe80::240:8cff:fe94:451e) from fe80::224:8cff:fe90:ad3b eth1: 56 data bytes
64 bytes from fe80::240:8cff:fe94:451e: icmp_seq=1 ttl=64 time=3.86 ms
64 bytes from fe80::240:8cff:fe94:451e: icmp_seq=2 ttl=64 time=0.471 ms
The code is below:
#include <linux/in6.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
void main()
{
int s, ret, err;
struct sockaddr_in6 addr;
s = socket(AF_INET6, SOCK_STREAM, 0);
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(554);
addr.sin6_flowinfo = 0;
addr.sin6_scope_id = 0;
addr.sin6_addr.s6_addr[0] = 0xfe;
addr.sin6_addr.s6_addr[1] = 0x80;
addr.sin6_addr.s6_addr[2] = 0x00;
addr.sin6_addr.s6_addr[3] = 0x00;
addr.sin6_addr.s6_addr[4] = 0x00;
addr.sin6_addr.s6_addr[5] = 0x00;
addr.sin6_addr.s6_addr[6] = 0x00;
addr.sin6_addr.s6_addr[7] = 0x00;
addr.sin6_addr.s6_addr[8] = 0x02;
addr.sin6_addr.s6_addr[9] = 0x40;
addr.sin6_addr.s6_addr[10] = 0x8c;
addr.sin6_addr.s6_addr[11] = 0xff;
addr.sin6_addr.s6_addr[12] = 0xfe;
addr.sin6_addr.s6_addr[13] = 0x94;
addr.sin6_addr.s6_addr[14] = 0x45;
addr.sin6_addr.s6_addr[15] = 0x1e;
ret = connect(s, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1)
{
err = errno;
printf("connect failure, errno = %d\n", err);
}
}
The result is always "connect failure, errno = 22"
Where is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要使用链接本地地址,则必须设置
sin6_scope_id
以匹配链接上网络设备的设备索引(这就是为什么您必须指定-我将 eth1
连接到您的ping6
命令)。您可以让
getaddrinfo()
为您完成所有艰苦的工作,包括设置作用域 ID(请注意地址末尾的%eth1
)和端口:If you're going to use a link-local address, you have to set the
sin6_scope_id
to match the device index of the network device on the link (this is why you have to specify-I eth1
to yourping6
command).You can have
getaddrinfo()
do all the hard work for you, including setting scope ID (note the%eth1
at the end of the address) and the port: