本文介绍了IPv6的:连接()总是失败,并将errno 22的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该操作系统是Ubuntu Linux系统。
我做了IPv6基本操作的简单测试。
PC正在与通过集线器的网络摄像机(支持IPv6)的连接。
ping6测试是成功的。

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

在code是如下:

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);
  }
}

结果总是连接失败,错误号= 22
问题出在哪里?

The result is always "connect failure, errno = 22"Where is the problem?

推荐答案

如果你要使用链路本地地址,你必须设置 sin6_scope_id 来匹配链路上的网络设备的设备指数(这就是为什么你必须指定 -I eth1的你的 ping6 命令)。

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 your ping6 command).

您可以的getaddrinfo()为你做所有的辛勤工作,包括设置范围ID(注意%的eth1 在地址的末端)和端口:

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:

struct addrinfo hints = { 0 };
struct addrinfo *res;
int gai_err;
int s;

hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;

gai_err = getaddrinfo("fe80::240:8cff:fe94:451e%eth1", "554", &hints, &res);

if (gai_err)
{
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_err));
    return 1;
}

s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (s < 0) {
    perror("socket");
    return 1;
}

if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
    perror("connect");
    return 1;
}

这篇关于IPv6的:连接()总是失败,并将errno 22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:30