LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2009, 12:32 AM   #1
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Rep: Reputation: 15
multicasting with different interface ..


hi friends,

can u please point a good example of how to change the interface of multicasting in unix socket programming ..

i succeed in sending message to multicast group members but i dont know how to change the interface ...

i use IP_MULTICAST_IF but i am still newbie to use it clearly in the code ... please help me ...
 
Old 06-23-2009, 05:34 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
You need to pass a "in_addr" struct to the "setsockopt (IP_MULTICAST_IF)" call, the "in_addr" must be filled by the address of the interface you want to use. To get it from the interface name, you can take a look to the SIOCGIFADDR and SIOCGIFCONF ioctl's or using the "getifaddrs" function.
 
Old 06-23-2009, 08:22 AM   #3
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Original Poster
Rep: Reputation: 15
thanks for reply ..
i succeed in setting different interface ..
Code:
 multi.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
 multi.imr_interface.s_addr = inet_addr("192.168.2.235");
 

 if(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&multi.imr_interface.s_addr,
              sizeof(multi.imr_interface.s_addr)) < 0)
        perror("IP_MULTICAST");
its working for second interface ...ie IP: 192.168.2.72 received it perfectly ... but IP: 192.168.1.68 receive nothing ..why ..?

i thought both class 192.168.2.72 and 192.168.1.68 will receive the multicast dgrams ...

please help ...

Last edited by wagmare; 06-23-2009 at 08:23 AM.
 
Old 06-23-2009, 09:00 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
The IP_MULTICAST_IF setsockopt is for the output only. To set the MULTICAST to receive datagram on a specified interface, you have to precise which one to use when requesting the membership with the IP_ADD_MEMBERSHIP setsockopt call.
 
Old 06-24-2009, 12:19 AM   #5
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Original Poster
Rep: Reputation: 15
Thumbs up

see my first receiver .. 192.168.2.72
client1.c
Code:
#define HELLO_GROUP = "226.0.1.32"
 saddr.sin_family = PF_INET;
        saddr.sin_port = htons(4096); // listen on port 4096
        saddr.sin_addr.s_addr = htonl(INADDR_ANY); 
 status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));


 imreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);

 imreq.imr_interface.s_addr = inet_addr("192.168.2.72"); // use second interface


 // JOIN multicast group on second interface
        status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
              (const void *)&imreq, sizeof(struct ip_mreq));
my second receiver ... 192.168.1.68
client2.c
Code:
  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));

 imreq.imr_multiaddr.s_addr = inet_addr("226.0.1.32");

   imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface


  // JOIN multicast group on default interface
   status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
              (const void *)&imreq, sizeof(struct ip_mreq));
see here only client1 is receiving multicast dgrams .... i am specifying the group of same class D IP .. for both client1.c and client2.c .. but only client1.c where receiving interface is 192.168.2.72(...2.72) can receive ...

please help

Last edited by wagmare; 06-24-2009 at 12:21 AM.
 
Old 06-24-2009, 07:32 AM   #6
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jf.argentino View Post
The IP_MULTICAST_IF setsockopt is for the output only. To set the MULTICAST to receive datagram on a specified interface, you have to precise which one to use when requesting the membership with the IP_ADD_MEMBERSHIP setsockopt call.
in the code above .. before i am requesting membership i am setting interface as imreq.imr_interface.s_addr = INADDR_ANY in one
and imreq.imr_interface.s_addr = inet_addr("192.168.2.72"); // use second interface

but only second interface 192.168.1.72 is working for client if server interface multicast on 192.168.2.x ..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about IP multicasting. lazareth1 Linux - Networking 1 05-30-2008 05:27 PM
multicasting info stoney55 Linux - Networking 3 03-08-2005 11:29 AM
Multicasting Problem Rajesh_Amma Linux - Networking 0 11-22-2004 02:41 AM
guide for multicasting bhupee Linux - General 1 07-01-2004 07:15 PM
Multicasting puzzle tachyon273 Linux - Networking 0 03-14-2003 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration