SPUG: Socket daemons

Timothy A. Gregory tgregory at tarjema.com
Fri Jun 22 19:32:36 CDT 2001


I was missing something simple, and I found it - I just did this:

while ($client = $server->accept()) {
        &process
        }

and put all the XML stuff in 

sub process {
...
}

been a long week.

--tag



On Fri, 22 Jun 2001, the heavens parted and Timothy A. Gregory said:

> I'm sure I'm missing something very simple here, please point it out to me
> ;-)
> 
> I've written a small script that I want to have sit listening to a port
> and stay there.  If a connection comes in it will process the data (an XML
> stream to be stuffed into a database). Right now it quits at that point,
> but I want it to stay running so the 10 other clients trying to send it
> XML reports will have someone to talk to.
> 
> listener.pl:
> 
> #!/usr/bin/perl -w
> use strict;
> use IO::Socket;
> use DBI;
> use XML::XPath;
> 
> my $server = IO::Socket::INET->new(
>   Listen => 10,
>   LocalAddr => '192.168.1.222',
>   LocalPort => 8888,
>   ReuseAddr => 1,
>   Proto => 'tcp'
> ) or die "Can't create server socket $!\n";
> 
> my $client = $server->accept;
> 
> my $xp = XML::XPath->new(ioref => $client);
> 
> my $dbh = DBI->connect('dbi:mysql:mon', 'monitor', 'sillypass')
>   or die $DBI::errstr;
> 
> my $sth = $dbh->prepare(qq{INSERT INTO stats VALUES (?, ?, ?, ?, ?, ?)});
> 
> foreach my $row ($xp->findnodes('/semaphore:net/output')) {
>   my $cust_name = $row->find('cust_name')->string_value;
>   my $host_name = $row->find('host_name')->string_value;
>   my $ip_addr = $row->find('ip_addr')->string_value;
>   my $up_down = $row->find('up_down')->string_value;
>   my $pmin = $row->find('pmin')->string_value;
>   my $pmax = $row->find('pmax')->string_value;
> 
>   $sth->execute($cust_name, $host_name, $ip_addr, $up_down, $pmin, $pmax)
>     || die $DBI::errstr;
> 
> }
> 
> Thanks!
> 
> --tag
> 
> 
> 
> +--------------------------------------------------+
> | Timothy A. Gregory        http://www.tarjema.com |
> | *NIX SysAdmin               tgregory at tarjema.com |
> | Arabic > English Translator         IBM AIX CATE |
> +--------------------------------------------------+
> Habit is habit and not to be flung out of the window by any man, but
> coaxed downstairs a step at a time.
> --Mark Twain
> ------------ Output from gpg ------------
> gpg: Good signature from "Timothy A. Gregory <tgregory at tarjema.com>"
> 
> 

+--------------------------------------------------+
| Timothy A. Gregory        http://www.tarjema.com |
| *NIX SysAdmin               tgregory at tarjema.com |
| Arabic > English Translator         IBM AIX CATE |
+--------------------------------------------------+
Habit is habit and not to be flung out of the window by any man, but
coaxed downstairs a step at a time.  
--Mark Twain


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list