in reply to how can N client.pl connect to one deamon serving mysql

may be sth like this will work?!

#!/usr/bin/perl

use IO::Select;
use IO::Socket::INET;

use ResourcePool;
use ResourcePool::Factory::DBI;
use ResourcePool::Command::DBI::Execute;


# Initialize parameter ------------------------------------------------

$debug= 1;
$main = new IO::Socket::INET (
      LocalHost => '127.0.0.1',
      LocalPort => 6000,
      Listen => 5,
      Proto => 'tcp',
      Reuse => 1 ) || die $!;
$zero = chr(0);
$/ = $zero;
$\ = $zero;
$| = 1;
my $dsn = "DBI:mysql:DBNAME;your.server#2;3306";
my $username = "xxx";
my $passwd = "zzz";

# Initialize Ressource ------------------------------------------------

my $factory = ResourcePool::Factory::DBI->new(
      $dsn,
      $username,
      $passwd);
my $pool = ResourcePool->new($factory, MaxTry => 3);


# Initialize IO::Select ------------------------------------------------

$handles = new IO::Select();
$handles->add($main);

print "Starting listening cycle\n" if($debug > 0);

LISTEN: while (1)
   {
      ($pending) = IO::Select->select($handles, undef, undef, 60);
      foreach $sock (@$pending)
      {
         if ($sock == $main)
         {
            $num++;             print "Got new connection: $num from ".$sock->sockhost()."\n" if($debug > 0);
            my $newsock = $sock->accept();
            $newsock->autoflush();
            $number{$newsock} = $num;
            $handles->add($newsock);
         } else {
            my $buf = <$sock>;
            if ($buf)
            {
               print "Existing socket $number{$sock} is pending: " if($debug > 0);
               chomp $buf;
               print "$buf\n" if($debug > 0);
               my $cmd = ResourcePool::Command::DBI::Execute->new();
               $pool->execute($cmd, $buf);
            } else {
               print "Socket $number{$sock} is gone.\n" if($debug > 0);
               $handles->remove($sock);
            }
         }
      }
   }

  • Comment on Re: how can N client.pl connect to one deamon serving mysql

Replies are listed 'Best First'.
Re^2: how can N client.pl connect to one deamon serving mysql
by Anonymous Monk on Feb 22, 2006 at 17:12 UTC
    i tried this with

    my $port = 6000;
    my $host = 'localhost';
    my $proto= getprotobyname('tcp');
    my $iaddr = inet_aton($host);
    my $paddr = sockaddr_in($port, $iaddr);

    my $code = 'INSERT INTO test (r1, r2, r3) VALUES (1,2,3);\n';

    socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
    connect(SOCK, $paddr) || die "connect: $!";
    print SOCK $code;
    close (SOCK) || die "close: $!";


    but after one connection attempt succeeded, the second one kills the deamon.pl:

    linux:/srv/www/system # ./deamon.pl &
    1 7336
    linux:/srv/www/system # ./client.pl
    linux:/srv/www/system # ./client.pl
    1+ Stopped ./deamon.pl
    linux:/srv/www/system #


    ...strange... :-( ...i use a logfile for the data the deamon.pl receives, and the first connections data was logged; but not the second ones....