I am attempting to listen and continually grab incoming UDP traffic on port 162 as shown below, however, we never seem to enter the while loop.
The traffic is visible via tcpdump on port 162 and the Perl code below is running as root. The script seems to be waiting and listening but, never any output.
Surely missing a key bit. Any ideas?
use IO::Socket::INET;
# flush after every write
$| = 1;
my $received;
my ($peeraddress, $peerport);
my $socket = new IO::Socket::INET(
LocalAddr => 'localhost',
LocalPort => '162',
Proto => 'udp',
Type => SOCK_DGRAM,
) or die "ERROR in Socket Creation : $@\n";
while ( $socket->recv($received, 1024) ) {
$peeraddress = $socket->peerhost();
$peerport = $socket->peerport();
print "\n($peeraddress , $peerport) said : $received";
}
$socket->close();
EDIT: the above code works when 'localhost' is replaced with '0.0.0.0', please see @ikegami 's answer. Also had the problem of traffic not reaching it because of the system firewall, as noted in comments.