UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Networking
  • » setsockopt returns No device Found error in multicast

#1 2009-01-27 10:50 AM

epolluser
Member
Registered: 2008-01-14
Posts: 21

Re: setsockopt returns No device Found error in multicast

Offline

#2 2009-01-27 12:20 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: setsockopt returns No device Found error in multicast

Could you post a bit more code, especially the part before where you do other things and initialize imreq? Or can it be found at that url? (No time to check it out now.)

Offline

#3 2009-01-27 01:30 PM

epolluser
Member
Registered: 2008-01-14
Posts: 21

Re: setsockopt returns No device Found error in multicast

server.c
___________________________________________________________

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte

int main()
{
   int sock, status, socklen;
   char buffer[MAXBUFSIZE];
   struct sockaddr_in saddr;
   struct ip_mreq imreq;

   // set content of struct saddr and imreq to zero
   memset(&saddr, 0, sizeof(struct sockaddr_in));
   memset(&imreq, 0, sizeof(struct ip_mreq));

   // open a UDP socket
   sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   if ( sock < 0 )
     perror("Error creating socket"), exit(0);

   saddr.sin_family = PF_INET;
   saddr.sin_port = htons(4096); // listen on port 4096
   saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
   status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));

   if ( status < 0 )
     perror("Error binding socket to interface"), exit(0);

   imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
   //imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
===>   imreq.imr_interface.s_addr = inet_addr("x.x.x.x"); // ip of the machine on which client is running
   // JOIN multicast group on default interface
   status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
              (const void *)&imreq, sizeof(struct ip_mreq));

   socklen = sizeof(struct sockaddr_in);

   // receive packet from socket
   status = recvfrom(sock, buffer, MAXBUFSIZE, 0, 
                     (struct sockaddr *)&saddr, &socklen);

   // shutdown socket
   shutdown(sock, 2);
   // close socket
   close(sock);

   return 0;
}

client.c
___________________________________________________________

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte

int main()
{
   int sock, status, socklen;
   char buffer[MAXBUFSIZE];
   struct sockaddr_in saddr;
   struct in_addr iaddr;
   unsigned char ttl = 3;
   unsigned char one = 1;

   // set content of struct saddr and imreq to zero
   memset(&saddr, 0, sizeof(struct sockaddr_in));
   memset(&iaddr, 0, sizeof(struct in_addr));

   // open a UDP socket
   sock = socket(PF_INET, SOCK_DGRAM, 0);
   if ( sock < 0 )
     perror("Error creating socket"), exit(0);

   saddr.sin_family = PF_INET;
   saddr.sin_port = htons(0); // Use the first free port
   saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
   status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));

   if ( status < 0 )
     perror("Error binding socket to interface"), exit(0);

// iaddr.s_addr = INADDR_ANY; // use DEFAULT interface 
  ===> iaddr.s_addr = inet_addr("x.x.x.x"); // ip of the machine on which client is running
   // Set the outgoing interface to DEFAULT
   setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iaddr,
              sizeof(struct in_addr));

   // Set multicast packet TTL to 3; default TTL is 1
   setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
              sizeof(unsigned char));

   // send multicast traffic to myself too
   status = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP,
                       &one, sizeof(unsigned char));

   // set destination multicast address
   saddr.sin_family = PF_INET;
   saddr.sin_addr.s_addr = inet_addr("226.0.0.1");
   saddr.sin_port = htons(4096);

   // put some data in buffer
   strcpy(buffer, "Hello world\n");

   socklen = sizeof(struct sockaddr_in);
   // receive packet from socket
   status = sendto(sock, buffer, strlen(buffer), 0,
                     (struct sockaddr *)&saddr, socklen);

   // shutdown socket
   shutdown(sock, 2);
   // close socket
   close(sock);

   return 0;
}

Offline

#4 2009-01-27 02:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: setsockopt returns No device Found error in multicast

Offline

#5 2009-01-28 07:17 AM

epolluser
Member
Registered: 2008-01-14
Posts: 21

Re: setsockopt returns No device Found error in multicast

Offline

#6 2009-01-28 02:22 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: setsockopt returns No device Found error in multicast

Offline

#7 2009-01-29 02:01 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: setsockopt returns No device Found error in multicast

No idea why, but printf seems to cause illegal seeks all the time...

Offline

#8 2009-01-29 11:16 AM

epolluser
Member
Registered: 2008-01-14
Posts: 21

Re: setsockopt returns No device Found error in multicast

in interface if i use the respective local ips of the machines where client and server are running the problem gets solved temporarily i.e. datagrams are being sent from server to client successfully.
Still the one query remains why does interface => INADDR_ANY does not work??
setsockopt with IP_ADD_MEMBERSHIP fails with No such Device error errno = 19 and though setsockopt  with IP_MULTICAST_IF  does not throw error sento call returns error Network Unreachable errno = 101.

Offline

#9 2009-01-29 10:22 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: setsockopt returns No device Found error in multicast

Offline

#10 2009-01-30 12:22 PM

josealf
Guest

Re: setsockopt returns No device Found error in multicast

route add -net 224.0.0.0 netmask 224.0.0.0 eth0

#11 2009-02-02 10:43 AM

epolluser
Member
Registered: 2008-01-14
Posts: 21

Re: setsockopt returns No device Found error in multicast

thanks a lot. this worked for me.. :)

Offline

#12 2013-03-22 05:25 PM

Merlin
Guest

Re: setsockopt returns No device Found error in multicast

Yeah, I was looking for this for hours! Thanks

#13 2013-07-24 01:35 PM

AwaX
Guest

Re: setsockopt returns No device Found error in multicast

Exactly the same problem, with the same example and your solution worked for me too ! Thanks a lot for your help guys !

#14 2014-04-28 10:56 AM

PangMin
Guest

Re: setsockopt returns No device Found error in multicast

I got this error, and checked the route table, add a default route to the working interface, the error is gone.

#15 2015-08-05 07:33 PM

Abdul Hakkim
Guest

Re: setsockopt returns No device Found error in multicast

Thanks a lot, Its amazingly working...

  • Index
  • » Networking
  • » setsockopt returns No device Found error in multicast

Board footer

Powered by FluxBB