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

Hi Everyone! I want to create a udp server, which listens for clients, and then reply to them, i have written a sub function for it, but it doesn't listen, and exits with a warning like this:Can't call method "recv" on an undefined value at .. line 114 the sub function is like this:
sub listen{ use IO::Socket; my $port1 = @_; $proto = 'udp'; $socket1 = new IO::Socket::INET (LocalPort => $port1,Proto=> $proto)or + die "Cannot Listen On The Socket $@"; print " Waiting for connection\n"; my $MAX_TO_READ = 512; my $datagram; while (1){ $server->recv($datagram,$MAX_TO_READ); if($datagram ne '') { print "\nReceived message '", $datagram,"'\n"; } # If client message is empty exit else { print "Cilent has exited!"; exit 1; } return $server; } }
and i am calling it like this: &rdt_listen($ARGV[1]) thanx in advance

Replies are listed 'Best First'.
Re: UDP Server
by Your Mother (Archbishop) on Dec 01, 2008 at 23:26 UTC

    The error message is pretty self-explanatory. $server is undefined so you can't call ->recv on it. Nowhere in your sample is $server declared or created. Take a look at $socket1. :)

    You are using strict and warnings, right? These kinds of developer errors aren't impossible with them on but they are a lot easier to avoid.

      how stupid of me,thanx, i m in hurry so i didn't c it,