Monky Python has asked for the wisdom of the Perl Monks concerning the following question:

tried to read data from a multicast port but I don't get any data.
The connection is established and the program seems to wait for data, but even if another process
sends some data to this address nothing happens.
I already checked with some Java and a C program that the server process is working correctly.
Any ideas?

my $udpcon  =  "227.50.49.48:9897";

my $sock1 = IO::Socket::INET->new(Proto=>'udp',PeerAddr=>$udpcon);
die "Can't bind to $udpcon: $!\n" unless $sock1;

my $data;
my $len;

print "waiting for data on $udpcon ...\n";

while (1) {
  $len = recv($sock1, $data, 10, 0);
  print $len."\n";

}

Replies are listed 'Best First'.
Re: reading from multicast socket
by rasta (Hermit) on Nov 12, 2002 at 08:56 UTC
    There are a few mistakes in your code:

    1. You should specify local port and SO_BROADCAST flag
    2. Use $@ to output error message from new()
    3. recv returns the addres of sender

    So, here is my code:
    my $sock1 = IO::Socket::INET->new( LocalPort => 9897, PeerAddr => inet_ntoa(INADDR_BROADCAST), # you may use '127.0.0.1 +' for local host Proto => 'udp', LocalAddr => '127.0.0.1', Broadcast => 1 ); die "Can't bind to $udpcon: $@\n" unless $sock1; my $data; my $addr; print "waiting for data on $udpcon ...\n"; while (1) { $addr = recv($sock1, $data, 10, 0); }
Re: reading from multicast socket
by Chief of Chaos (Friar) on Nov 12, 2002 at 10:58 UTC
    Hi Monky,
    it is possible to use the 'IO::Socket::Multicast'-module.
    This may be a little bit easier.

    e.g.:
    #!/usr/bin/perl # client use strict; use IO::Socket::Multicast; use constant GROUP => '227.50.49.48'; use constant PORT => '9897'; my $sock = IO::Socket::INET->new(Proto=>'udp',LocalPort=>PORT); $sock->mcast_add(GROUP) || die "Couldn't set group: $!\n"; while (1) { my $data; next unless $sock->recv($data,1024); print $data; }
Re: reading from multicast socket
by amphiplex (Monk) on Nov 12, 2002 at 08:57 UTC
    Hi !

    shouldn't you be using LocalAddr instead of RemoteAddr ?

    ---- amphiplex