gpt4 book ai didi

c - IP_MULTICAST_IF 的默认值

转载 作者:行者123 更新时间:2023-11-30 18:01:18 25 4
gpt4 key购买 nike

我试图弄清楚,在设置新套接字后,IP_MULTICAST_IF 选项使用什么默认值。不幸的是我的代码出现了段错误,我实际上不知道为什么?!我做错了什么吗?或者如何获取 IP_MULTICAST_IF 选项的默认值?

  int sock;
struct in_addr interface_addr;
int addr_size;

if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket() failed");
}

addr_size = sizeof(interface_addr);

if ((getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, &addr_size)) < 0) {
perror("getsockopt() failed");
}

printf("The default interface is %s\n", inet_ntoa(interface_addr));

最佳答案

阅读适合您平台的 ip(7) 手册页。 IP_MULTICAST_IF 不接受 struct in_addr

在 Linux 上:

IP_MULTICAST_IF (since Linux 1.2)
Set the local device for a multicast socket. Argument is an ip_mreqn or
ip_mreq structure similar to IP_ADD_MEMBERSHIP.
When an invalid socket option is passed, ENOPROTOOPT is returned.

IP_ADD_MEMBERSHIP (since Linux 1.2)
Join a multicast group. Argument is an ip_mreqn structure.

struct ip_mreqn {
struct in_addr imr_multiaddr; /* IP multicast group
address */
struct in_addr imr_address; /* IP address of local
interface */
int imr_ifindex; /* interface index */
};
imr_multiaddr contains the address of the multicast group the application wants to join or
leave. It must be a valid multicast address (or setsockopt(2) fails with the error EINVAL).
imr_address is the address of the local interface with which the system should join the multi‐
cast group; if it is equal to INADDR_ANY an appropriate interface is chosen by the system.
imr_ifindex is the interface index of the interface that should join/leave the imr_multiaddr
group, or 0 to indicate any interface.
The ip_mreqn structure is available only since Linux 2.2. For compatibility, the old ip_mreq
structure (present since Linux 1.2) is still supported; it differs from ip_mreqn only by not
including the imr_ifindex field. Only valid as a setsockopt(2).

关于c - IP_MULTICAST_IF 的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9869732/

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