#!/usr/bin/perl
# (1) quit unless we have the correct number of command-line args
use strict;
use warnings;
use IO::Socket::INET;
use IO::Select;

my $num_args = $#ARGV + 1;
if ($num_args != 2) {
    print "\nUsage: check-udp-connection.pl host port\n";
    exit;
}

# (2) we got two command line args, so assume they are the
# first name and last name
my $address=$ARGV[0];
my $port=$ARGV[1];

print "Hello, $address $port\n";
scanUDP("$address","$port");
sub scanUDP {
    my $address = shift;
    my $port = shift;
    my $socket = new IO::Socket::INET (
                                        PeerAddr => $address,
                                        PeerPort => $port,
                                        Proto => 'udp',
                                        ) or return 0;
    $socket -> send('Hello',0);
    my $select = new IO::Select();
    $select -> add($socket);
    my @socket = $select -> can_read(1);
##print scalar @socket;
    if (@socket == 1) {
        $socket -> recv(my $temp, 1, 0) or return 0;
        return 1;
    }
    return 1;
}

