Sorry. Here is the full code of the script. It tests if a SIP server is online or not. I tried to replace recv with recv_timeout as suggested but it no longer returns the data sent back by the UDP VoIP server:
# !/usr/bin/perl use IO::Socket; use POSIX 'strftime'; use Time::HiRes qw(gettimeofday tv_interval); use Getopt::Long; my $USAGE = "Usage: sip_ping.pl [-v] [-t] [-s <src_host>] [-p <src_por +t>] <hostname>"; my $RECV_TIMEOUT = 5; # temps d'attente d'une réponse en secondes my $sock; eval { $sock = IO::Socket::INET->new(Proto => 'udp', LocalPort=>'6655' +, ReuseAddr=>1) }; if ($@) { print "Socket could not be made $@"; } else { # options my ($verbose, $host, $my_ip, $my_port ,$time); #GetOptions("Verbose|v" => \$verbose, "source-ip|s=s" => \$my_ip, +"source- port|p=n"=> \$my_port, "time|t" => \$time) or die "Options i +nvalides:\n\n$USAGE\n"; #$my_ip="127.0.0.1"; #$my_port="5060"; # détermine à qui envoyer un ping $verbose=1; $time=1; my $host = "freevoip.fonosip.com"; my $dst_addr; my $portaddr; eval { $dst_addr = inet_aton($host); }; if ($dst_addr eq "") { print "Host $host not found\n"; } else { my $dst_ip = inet_ntoa($dst_addr); eval { $portaddr = sockaddr_in(5060, $dst_addr); }; if ($@) { print "No connection could be made on port 5060 to $host\n +"; } else { # détermine qui nous sommes $my_ip = "127.0.0.1" unless defined($my_ip); $my_port = "6655" unless defined($my_port); # Numéro appelant long de 32 caractères hexadécimaux aléat +oires my $callid = ""; $callid .= ('0'..'9', "a".."f")[int(rand( +16))] for 1 .. 32; # date du jour my $date = strftime('%a, %e %B %Y %I:%M:%S %Z',localtime() +); # id de branche - voir la rfc3261 pour plus d'infos, avec # utilisation de time() pour l'unicité #Contact: <sip:ping\@$my_ip> my $branch="z9hG4bK" . time(); my $packet = qq( REGISTER sip:$my_ip SIP/2.0 Via: SIP/2.0/UDP $my_ip:$my_port;branch=$branch From: <sip:810122009\@freevoip.fonosip.com> To: <sip:810122009\@freevoip.fonosip.com> Contact: <sip:810122009\@freevoip.fonosip.com> Call-ID: $callid\@$my_ip CSeq: 0 REGISTER max-forwards: 10 expires: 60 User-Agent: ServersCheck Monitoring Date: $date Allow: ACK, CANCEL Content-Length: 0 ); # envoie le paquet print "Envoi: \n\n$packet\n" if $verbose; eval { send($sock, $packet, 0, $portaddr) == length($packe +t) }; if ($@) { print "Could not send data to $host\n"; } else { my $send_time = [gettimeofday()]; # démarre le chronom +ètre my $elapsed; $RECV_TIMEOUT=5; # obtient la réponse eval { local $SIG{ALRM} = sub { die "temporisation de l'a +larme" }; alarm $RECV_TIMEOUT; $portaddr = recv($sock, $packet, 1500, 0) or die " +Réception impossible: $!"; alarm 0; 1; } or die($@); # affiche la sortie $elapsed = tv_interval($send_time); # arrête le chrono + alarm 0; if ($verbose) { my @data=("time: xx\%0.2fxx, answer:\n", $elapsed*1000, $packet); foreach (@data) { print "data: $_\n"; } } elsif ($time) { printf("%0.2f\n", $elapsed*1000); } else { print("$host est en fonctionnement\n"); } } } } }

In reply to Re^4: Timeout on UDP receiving (Windows) by ikkeniet
in thread Timeout on UDP receiving (Windows) by ikkeniet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.