当前位置:  开发笔记 > Android > 正文

组播 - 没有这样的设备

如何解决《组播-没有这样的设备》经验,为你挑选了2个好方法。

我正在尝试使用以下代码连接到多播组:

 int flag_on = 1;              /* socket option flag */
  struct sockaddr_in mc_addr;   /* socket address structure */
  char recv_str[MAX_LEN+1];     /* buffer to receive string */
  int recv_len;                 /* length of string received */
  char* mc_addr_str;            /* multicast IP address */
  unsigned short mc_port;       /* multicast port */
  struct sockaddr_in from_addr; /* packet source */
  unsigned int from_len;        /* source addr length */


  mc_addr_str = ip;      /* arg 1: multicast ip address */
  mc_port = port;    /* arg 2: multicast port number */

  /* validate the port range */
  if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
    fprintf(stderr, "Invalid port number argument %d.\n",
            mc_port);
    fprintf(stderr, "Valid range is between %d and %d.\n",
            MIN_PORT, MAX_PORT);
    exit(1);
  }

  /* create socket to join multicast group on */
 // if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  if ((sock = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP)) < 0) {
    perror("socket() failed");
  LOGE("*********Inside Join Multicast -- socket() failed*********");
    exit(1);
  }
  LOGE("Socket value  = %d ",sock);
  /* set reuse port to on to allow multiple binds per host */
  if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
       sizeof(flag_on))) < 0) {
    perror("setsockopt() failed");
  LOGE("*********Inside Join Multicast -- socketopt() failed*********");

    exit(1);
  }

  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  mc_addr.sin_port        = htons(mc_port);

  /* bind to multicast address to socket */
  if ((bind(sock, (struct sockaddr *) &mc_addr,
       sizeof(mc_addr))) < 0) {
    perror("bind() failed");
  LOGE("*********Inside Join Multicast -- bind() failed*********");
    exit(1);
  }

  /* construct an IGMP join request structure */
  mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
  mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

  /* send an ADD MEMBERSHIP message via setsockopt */
  if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
       (void*) &mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
  LOGE("*********Inside Join Multicast -- socketopt2() failed*********");
  LOGE("Value of errno is %s",strerror(errno));
 exit(1);
  }

我收到的错误是errno的值是没有这样的设备.

我试图在omap板上实现这一点 - GB移植.

能否请你帮忙.



1> Alexey..:

我有一个非常相似的问题,虽然我使用的是java界面.在我的情况下,在我明确说明哪个接口应该处理多播数据包之前,我得到"没有这样的设备"错误.就我而言,那是一个以太网接口.再次这不安静你的情况,因为你正在使用JNI,并且因为你可能不需要eth0,但我希望它会有所帮助:

Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
NetworkInterface eth0 = null;
while (enumeration.hasMoreElements() {
    eth0 = enumeration.nextElement()
    if (eth0.getName().equals("eth0")) {
        //there is probably a better way to find ethernet interface
        break;
    }
}

InetAddress group = InetAddress.getByName(IP);
MulticastSocket s = new MulticastSocket(PORT);
s.setSoTimeout(10000);
//s.joinGroup(group); //this will throw "No such device" exception 
s.joinGroup(new InetSocketAddress(group, PORT), eth0); // this works just fine

for (int i = 0; i < 10; ++i) {
    byte[] buf = new byte[8096];
    DatagramPacket recv = new DatagramPacket(buf, buf.length);
    s.receive(recv);
    System.out.println("Recieved " + recv.getLength() + " bytes.");
}

s.leaveGroup(group);

所以我想这个想法是,如果你有超过1个接口,你应该明确指定你使用的是哪一个.



2> 小智..:

您可能没有多播流量的路由.试试:

route add -net 224.0.0.0 netmask 224.0.0.0 dev eth0

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有