繁体   English   中英

IP_MULTICAST_IF的默认值

[英]default values for IP_MULTICAST_IF

我试图弄清楚设置新的套接字后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). 

我的错,我的代码缺少包含。

#include <arpa/inet.h>

它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM