简体   繁体   中英

Testing multicast with perl PortState

I'm writing a program which creates a network between processes and one of them is like a "coordinator". The coordinator should be able to check if some other process is dead or alive in the network. I wrote a routine (permanently executed by a thread) that checks this by seeing if the process is listening from a multicast port (by multicast port I mean a common port, let's say 2000, between processes of the same multicast group, and the connection when a message is send uses "udp" protocol) using IO::Socket::PortState :

my %port_hash = ( udp => {'2000' => {} } );
my $timeout = 5;
while (1) {
    #  checking system node
    my $host_hr = check_ports($node_host, $timeout, \%port_hash); 
    my $isAlive = $host_hr->{udp}{'2000'}{open};
    if (!$isAlive) { 
        print "$node_host died";
    } else { print "Everything OK with $node_host"; }
}

Then when I lift two or more processes, it works fine, it says "Everything OK with host_X", but if I kill the node "host_X" it's still printing the same thing. (So, it never recognizes when a process is dead).

I don't see what the problem is, if I do that with a non-multicast port it works perfectly, so I'm guessing it has to be with the fact that it is a multicast port.

Is there a way to get this to work with that kind of port? And if it's impossible to do, what's the best way to check if another process is alive taking into consideration that the coordinator has to check a LOT of processes.

Note : Assume that all the multicast stuff works fine (It sends messages the way it should)

This (6 year stale) bug report for IO::Socket::PortState says that checking is broken for UDP.

However, since the library is very tiny--it's actually a single function of about 30 lines, instead of bringing another dependency into project, I'd implement the check myself, probably using Perl core module IO::Socket::INET .

I can't tell you how exactly to do it right, but if you read the library code, you will see how it's done in the library. However, you will still need to fix the bug for UDP to work. After you do that, consider creating a patch for the library and sending it to maintainer (use links on the module page).

Some more general notes on the checking:

If the point is to check that the machine is reachable (ie online and routable), ICMP (ping) from the internet layer should be enough. Using higher layer does not tell you more unless you are probing the same port you are willing to communicate to, and cost much more overhead (at least for TCP, maybe it's better for UDP but it's still more.)

Later when you get back to this and try to implement more "proper" check, I recommend reading Port scanning basics on Nmap site. (Come to think of it, there's also an Nmap library in CPAN, bout that would be definitely a bazooka to shoot a fly.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM