simple socket programming in perl

hi

i want to write simple socket program which will listen on socket . here is the code

## read msg on socket 
#! /usr/bin/perl

use IO::Socket::INET;

my $MySocket= IO::Socket::INET->new(LocalPort=>1234,
                                                       Proto=>'udp') ;
while () {
   $MySocket->recv($text,128);
   if ( $text =~ /Done/i )
   {
      print " ftpdone\n"  ;
   } else
   {
      print " FTP Failed :Msg from Server [$text]\n" ;
      exit 1 ;
   }
}

## send message to socket
use IO::Socket::INET;
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,
                                    Proto=>'udp',
                                    PeerAddr=>'zzz.zzz.zz.zz');

$msg="ftp failed" ;
$MySocket->send($msg);

now i want is that receiver program should try to read on port for 10 sec if no msg is received then wait for minute and again check for message for 10 sec do this for 10 minutes ( inshort check if any message is received in 10 minutes )

but code waits at

$MySocket->recv($text,128);

line for mesg till it receives msg. Timeout => 10 is not working
what else i can do ? any other module ?? or any work around ??

Under Linux there is a way to make any file handler / file descriptor non-block through the fcntl() function.

use POSIX;
...
fcntl($MySocket, F_SETFL(), O_NONBLOCK()); # call this before $MySocket->recv($text,128)
...

You can also use sysread() which won't block.

IO::select is also a standard unix sockets approach.