当我使用sendto()
将数据报发送到不存在的目的地时,我发现 IPv4 和 IPv6 的结果不同。
IPv4:它只返回一个正值。
IPv6:返回
-1
并errno
设置为ENETUNREACH
有谁知道为什么会这样?
这是我的代码:
int main (int argc, char *argv[])
{
// Usage: program [version]
int version = argc == 1 ? 4 : atoi(argv[1]);
int fd = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (fd == -1)
ErrAndExit ("socket");
if (version == 4) // use ipv4
{
struct sockaddr_in srv_addr;
memset (&srv_addr, 0, sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons (11111);
if (inet_pton (AF_INET, "192.168.0.200", &srv_addr.sin_addr) != 1)
ErrAndExit ("inet_pton");
socklen_t len = sizeof(srv_addr);
puts("going to sendto...");
ssize_t res = sendto (fd, "hello", 6, 0, (struct sockaddr*) &srv_addr, len);
if (res == -1)
ErrAndExit("sendto");
printf ("done with res: %ld\n", res);
}
else // use ipv6
{
struct sockaddr_in6 srv_addr;
memset (&srv_addr, 0, sizeof(srv_addr));
srv_addr.sin6_family = AF_INET6;
srv_addr.sin6_port = htons (11111);
if (inet_pton (AF_INET6, "2002::148:249", &srv_addr.sin6_addr) != 1)
ErrAndExit ("inet_pton");
socklen_t len = sizeof(srv_addr);
puts("going to sendto...");
ssize_t res = sendto (fd, "hello", 6, 0, (struct sockaddr*) &srv_addr, len);
if (res == -1)
ErrAndExit("sendto");
printf ("done with res: %ld\n", res);
}
return 0;
}