develooper Front page | perl.libnet | Postings from November 2008

How to bind to specific interface with IO::Socket::Molticast?

Josef Wolf
November 22, 2008 04:24
How to bind to specific interface with IO::Socket::Molticast?
Message ID:

On a multi-homed host, I want to create a separate socket for each
multicast interface.  So I have come up with the solution attached
below. This solution works fine, except for one problem: if a packet
is received on the given multicast-group, this packet is received on
_all_ sockets and not only on the one that is bound to the receiving

Any hints how to tell IO::Socket::Multicast that it should deliver
the packet only to the socket that listens on the receiving interface?

BTW: this is ubuntu-8.10 with Linux 2.6.27-7-generic

#! /usr/bin/perl

use strict;
use warnings;

use IO::Handle;
use IO::Select;
use IO::Socket::INET;
use IO::Socket::Multicast;
use IO::Interface::Simple;

my $rdfds = IO::Select->new();

my @ssdp_server;
foreach my $if (IO::Interface::Simple->interfaces) {
    my $adr=$if->address;
    next unless $adr =~ /^(192|172)/;
    next unless $if->is_multicast;

    print "$adr\n";
    my $socket = IO::Socket::Multicast->new (LocalAddr=>"",
                                             ReuseAddr=>1) or die;
    $socket->mcast_add("", $adr);

while (1) {
    my @ready=$rdfds->can_read (0);
    foreach my $fh (@ready) {
        print $fh->mcast_if, "\n";
        $fh->recv(my $data, 1024);
#        print "$data\n";
} Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About