ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
[Sovled!] RE: UDP Client {out of my comfort area}
by Bigal other posts by this author
Feb 21 2008 8:21PM messages near this date
view in the new Beta List Site
Re: discussing good style | UDP Client {out of my comfort area}
Thanks to Angelo for helping me solve the UDP Deadlock problem... here's the solution in cas
e it helps another... 

[BTW, Yes... thank you... I promise not to post such lengthy questions in the future...]

--udp-deadlock-solution-snip--
use strict;
use IO::Socket;
use IO::Select;

use constant MAX_MSG_LEN  =>  5000;
use constant TIMEOUT      =>  2;
use constant MAX_RETRIES  =>  5;
my $msg_in;

my $host = "runner.terramagnetoscope.net";
my $port = "5001";
my $initialmsg = "1|GEOMAGROUTER|UPROCID|22";
my $pingrsps = "4|GEOMAGROUTER|XYZCAP|22";

print "Setting Up Socket...\n";

my $sock = IO::Socket::INET-> new(Proto=>'udp',PeerPort => $port, PeerAddr=>"$host") or die $
@;

my $select = IO::Select-> new($sock);

print "Sending Initial [Helo] Msg \n";

$sock-> send($initialmsg) or die "INITIALMSG Send Failed... $!";

while (1) {
  
  
      my @ready = $select-> can_read(TIMEOUT);
      
      for my $handle (@ready) {
        
        $sock-> recv($msg_in, MAX_MSG_LEN) or die "Failed... recv(): $!\n";
        
        print "SVR: $msg_in\n";
        
        if ($msg_in =~ /^3\|/) {
            
            print "\n+++ Caught KeepAlive Ping Request From $host:$port +++  \n";
            print "\n+++ Replying with KeepAlive Response [$pingrsps]\n";
           $sock-> send($pingrsps) or die "Ping Reply Send Failed... $!";
           
          }
      }
   do_retries() unless @ready;
}

$sock-> close;

sub do_retries {
  
  print "Comms Appear To be Down... will try reseting comms...\n";
    my $xctr = 0;
    my $rslt;
    
    for (1...10) {
        $xctr++;
        $rslt = $xctr - 10 ;
        print "Comms Outage... Trying Again in $rslt seconds \n";
        sleep (1);
    }
    
    eval {$sock-> send($initialmsg) or die "INITIALMSG Send Failed... $!"; };
    
}
exit;
--end-of-solution-snip--

PS: The select is a pretty tight loop in reality that I don't really like but it solves the 
problem :> )

-BigAL
bigal@[...].com
bigal@[...].net

On Wed, 20 Feb 2008 21:21:31 -0700, "BigAl" <bigal@[...].com>  wrote:
> 
> Hi all,
> 
> I've got a challenge outside my comfort zone (UDP Sockets). 
> 
> I'm trying to implement a UDP data aggregation process model that allows
> reporting by autonomic, magnetic sensors <using less connectivity overhead>
> as some reporting magnetic sensors have a low (crappy) bandwidth capability
> (read Dial-Up or Crystal Ball Comms!).
> 
> I'm writing a UDP 'Client' that signals a UDP 'Server' when I (The Client)
> am alive and ready to ingest global, magnetic sensor data. The Challenge is,
> the UDP server requires the following connection criteria:
> 
>   1. Upon connect, I (client) send an initial "HELO Packet" in pipe
> delimited format <below> as directed by the server to announce my presence.
> 
>   2. Every 60 seconds, the 'Server' Sends an "Is Client Alive?" packet
> (IsAlive packet payload begins with ^4)... I (the client) need to respond
> with a packet beginning with ^21 (I'm still here) packet beginning with ^21.
> 
> 
> The Challenge:
> 
> I am trying to simulate network outages of up to 300 seconds <Using VMWARE
> Client for Testing as that's all I've got> as this is the MAX AVG (that I've
> calculated from various, historic... IO conn outages)... 
> 
> .... However the UDP 'Client' Below that I largely modeled from "Perl
> Cookbook => Chapt 17.4" seems to hang and it will not recover despite my
> best interpretations of the process model [$SIG{ALRM}]. I've tried wrapping
> the read using select as well but the system (OS) doesn't seem to recover. 
> 
> 
> Read("you're not catching us at our best" - Jim Kirk, Trek IV).
> 
> 
> The Potential Solution: I am trying to develop a UDP 'Client' that can
> withstand a network outage of up to 300 seconds... then re-connect by
> sending a new, initial "HELO Packet" and resuming the aggregation of many
> magsensor site data.
> 
> The current script seems to hang (infinitively) without dumping to the
> &sleep_wait sub, [time-waster].
> 
> Thanks for any insight... as I am really aggravated with this UDP thing
> (thinking about going back to TCP but realize UDP is best for low bandwidth
> clients)... This is a nano-version of the real app, way condensed due to
> complexity (and my crappy programming skills) ]
> 
> PS: I'd love to spank the "powershell" weenies in the "scripting games", but
> I've not been presented with a relevant challenge yet (provided it doesn't
> include UDP Stuff!) 
> 
> 
> --start-snip--
> use IO::Socket;
> use strict;
> 
> my ($initialmsg, $MSG, $sock, $server_host, $port, $ipaddr, $hishost,
> $MAXLEN, $PORTNO, $TIMEOUT);
> 
> $server_host = "192.168.10.77"; # cant publicly display actual IP for this
> problem, yes I realize this stinks... 
> $PORTNO = "5001";
> $MAXLEN = 1024;
> $TIMEOUT = 5;
> $initialmsg = "1|GEOMAGROUTER|datafetcher|22";
>   
> $sock = IO::Socket::INET->new(
>     
>     Proto => 'udp',
>     PeerPort => $PORTNO,
>     PeerAddr => $server_host) or die "Couldnt Create Initial Socket!! \n";
> 
>     # Send Initial HELO mesg
>     $sock->send($initialmsg) or die "INITIALMSG Send Failed... $!";
> 
>    while(1) {
>      
>             eval {
>              $SIG{ALRM} = sub { die "Alarm Called on TIMEOUT \n"};
>              alarm $TIMEOUT;
>              $sock->recv($MSG, $MAXLEN) or die "Oddball REC ERR $! \n";
>              alarm 0;
>              1;
>            } or &sleep_wait;
>             
>                 ($port, $ipaddr) = sockaddr_in($sock->peername);
>                 $hishost = gethostbyaddr($ipaddr, AF_INET);
>                 print "Server $hishost responded by sending: $MSG \n";
>    }
> 
> 
> sub sleep_wait {
>     
>      print "Comms Appear To be Down... will try reseting comms...\n";
>    
>    my $xctr;
>    my $rslt;
>    
>    for (1...10) {
>         $xctr++;
>         $rslt = $xctr - 10 ;
>         print "Comms Outage... Trying Again in $rslt seconds \n";
>         sleep (1);
>     }
> }
> --end-of-snip--
> 
> 
> 
> -BigAL
> 
> bigal@[...].net 
> bigal@[...].com 
> 
> http://www.terramagnetoscope.net {magnetic research is the way to the
> future!}
> http://www.mollensoft.com {private home of my 'serious' apps}
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  
> 
> 
> 
> _______________________________________________
> Perl-Win32-Users mailing list
> Perl-Win32-Users@[...].com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
> 
	



_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved