#!/usr/bin/perl


$server_host = shift;
$TIMEOUT = shift;
$ask = shift;
chomp $ask;
$options = shift;
chomp $options;

if ($TIMEOUT eq "") {
  print "Il faut au moins 3 paramètres :";
  print "\t1° paramètre = 'host' = adresse IP ou nom de serveur";
  print "\n";
  print "\t2° paramètre = 'timeout' = délai d'attente en secondes";
  print "\n";
  print "\t\texprimé en 'virgule flottante' : doit contenir un point : ex. : 0.1";
  print "\n";
  print "\t\texprimé en 'virgule flottante' : jusqu'à la nanoseconde !";
  print "\n";
  print "\t3° paramètre = nom du fichier dont on demande le contenu";
  print "\n";
  print "\n";
  print "\t4° paramètre (optionnel) = 'd' pour 'debug' : affichage des étapes";
  print "\t";
  print "\n";
  exit;
}

use IO::Socket;
use Time::HiRes;


$hdnb = `/bin/cat /etc/hd.nb`;
$msg = "$ask $hdnb";


$local_PORTNO  = 52001;
$server_PORTNO  = 52000;
$MAXLEN  = 1024;

# création du socket pour le transfert 'udp'
$sock = IO::Socket::INET->new(Proto     => 'udp',
                              LocalPort => $local_PORTNO,
                              PeerPort  => $server_PORTNO,
                              PeerAddr  => $server_host)
    or die "Creating socket: $!\n";

# transmettre $ask par udp :	
eval {
    local $SIG{ALRM} = sub { die "alarm time out" };
    Time::HiRes::alarm ($TIMEOUT);
$sock->send($msg) or die "send: $!";
    Time::HiRes::alarm 0;
    1;  # return value from eval on normalcy
} ;



# attendre la réponse udp :	
$start = Time::HiRes::time;
eval {
    local $SIG{ALRM} = sub { die "alarm time out" };
    Time::HiRes::alarm ($TIMEOUT);
    $sock->recv($answer, $MAXLEN)      or die "recv: $!";
    Time::HiRes::alarm 0;
    1;  # return value from eval on normalcy
} ;


print "$answer";
