Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

android - Multicast - no such device

I am trying to connect to a multicast group using the following piece of code:

 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.
",
            mc_port);
    fprintf(stderr, "Valid range is between %d and %d.
",
            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);
  }

and the error I have received is Value of errno is No such device.

I am trying to achieve this on omap board - GB ported.

Could you please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a very similar problem, although I was using java interface. In my case, I was getting "No such device" error until I explicitly stated which interface should be handling multicast packets. In my case, that was an ethernet interface. Again this is not quiet your case, since you're using JNI, and also since you probably don't need eth0, but I hope it'll help:

Enumeration<NetworkInterface> 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);

So I guess the idea is that if you have more than 1 interface, you should explicitly specify which one are you using.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...