我想弄清楚,在设置新套接字后,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));
答案 0 :(得分:0)
阅读适用于您平台的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).
答案 1 :(得分:0)
我的错,我的代码缺少一个包含。与
#include <arpa/inet.h>
它工作正常。