Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to properly send and receive data from multicast?

by Anonymous Monk
on Aug 31, 2024 at 09:32 UTC ( [id://11161465]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to multicast messages in a local network using IO::Socket::Multicast module. Here's my attempt:

sender.pl

use strict; use warnings; use IO::Socket::Multicast; my $socket = new IO::Socket::Multicast() or die "cannot create socket: + $!\n"; $socket->mcast_send("this is multicast", '239.1.1.1:9999') or die "err +or: $!\n"; $socket->close();

receiver.pl

use strict; use warnings; use IO::Socket::Multicast; my $socket = new IO::Socket::Multicast( LocalPort => 9999, ) or die "cannot create socket: $!\n"; my $res = $socket->mcast_add('239.1.1.1'); print "subscribe status: $res\n"; my $data; $socket->recv($data, 1024); print "received: $data\n"; $socket->close();

The problem is that the receiver couldn't receive the message, it just kept waiting.

Additionally, I tested the program on 2 machines, interchanging the roles of sender and receiver and probed them using Wireshark Only one machine displayed "membership report group 239.1.1.1" in the info column, while the other had different group IP, even though both machines printed "subscribe status: 1" in the command line. The machine that seemed to be correct is a Windows virtual machine hosted by the other Windows machine.

Why is this happening and how to multicast properly? Any help would be appreciated.

Replies are listed 'Best First'.
Re: How to properly send and receive data from multicast?
by Danny (Hermit) on Aug 31, 2024 at 19:21 UTC
    On the sender side if I do the following it works for me:
    my $socket = new IO::Socket::Multicast(Proto=>'udp', PeerAddr=>'239.1. +1.1:9999') or die "cannot create socket: $!\n"; $socket->send("this is multicast");
    See the EXAMPLE section of 'perldoc IO::Socket::Multicast'

      That's weird, it doesn't work for me at all. I copied the example from the document, but it also didn't work.

        Multicast may be blocked by your computer setup, or it might not be supported (or be actively blocked) by your network setup.

        You could run network analysis tools (for example Wireshark) on both ends of the connection to see if the packets are send correctly and if they get to the destination host.

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
        Also check out my sisters artwork and my weekly webcomics
Re: How to properly send and receive data from multicast?
by tybalt89 (Monsignor) on Sep 02, 2024 at 05:30 UTC

    Here's some working code on my main machine.

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11161465 use warnings; use IO::Socket::Multicast; my $s = IO::Socket::Multicast->new or die $@; $s->mcast_if( 'tybalt' ); $s->mcast_dest(scalar sockaddr_in(9999, inet_aton('239.1.1.1'))); my $me = qx(hostname) =~ tr/\n//dr; open my $la, '<', '/proc/loadavg' or die "$! opening loadavg"; while(1) { seek $la, 0, 0; my $data = "$me " . localtime . ' ' . <$la>; print "data: $data"; $s->mcast_send($data) or die "$! on send"; sleep 1; }
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11161465 use warnings; use IO::Socket::Multicast; my $s = IO::Socket::Multicast->new(LocalPort => 9999) or die $@; $s->mcast_add('239.1.1.1', 'tybalt') or die "group $!"; while(1) { $s->recv(my $data, 1024); print $data; }

    This machine has TWO interfaces, one of which is named 'tybalt' (sort of like 'eth0' but different :). This code would not work until I added the interface name in the appropriate places. On another similar machine that has only one interface, the interface name was not required. Neither was the interface name required on a raspberry pi running Manjaro.

    So my working theory is that if you have more than one interface, interface names are required.
    Interestingly, omitting the interface name from the mcast_add method for the two interface machine does not cause an error, it just doesn't get the multicast messages.

    Two machines are running ArchLinux and one is running Manjaro. All three can multicast to each other, and all three are receiving all multicasts.

    Note also that none of my machines would take a string in mcast_dest, nor would they take a string as the second argument of mcast_send.

      I think the problem lies with my network. When I checked the network settings in the default gateway address, all IGMP related options had already been turned on. I tried your solution, but nothing changed. I also tried using another real machine to test, but the problem still persisted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11161465]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-09-09 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.