in reply to Daemon with Net::Daemon
Define a new class that inherits from Net::Daemon in daemon.pl
In your class's constructor, call the Net::Daemon constructor and then do any other initialisation you need.require Net::Daemon; package Contdmon; use vars qw(@ISA); @ISA = qw(Net::Daemon); # to inherit from Net::Daem
Now write your Run method. This method overrides the Run method in Net::Daemon. It is executed whenever a client connects to the daemonsub new ($$;$) { my($class, $attr, $args) = @_; my($self) = $class->SUPER::new($attr, $args); # set up the logfile my $file = IO::File->new ( '/path', "a" ); $file->autoflush ( 1 ) ; $self->{'logfile'} = $file; $self; }
Now go ahead and create your serversub Run ($) { my($self) = @_; my($line, $result, $sock); $sock = $self->{'socket'}; while (1) { if (!defined($line = $sock->getline())) { if ($sock->error()) { $self->Error("Client connection error %s", $sock->error()); } $sock->close(); return; } } print "Server received request: $line\n"; }
So at this point you have a daemon sitting there waiting for a request. Run this script on server A.package main; my $args = { 'facility' => 'daemon', 'pidfile' => '/users/ccm_root/logs/contdaem.pid', 'logfile' => '/users/ccm_root/logs/contdaem.log', 'user' => 'ccm_root', 'group' => 'ccm_root', 'localport' => '5440', 'mode' => 'single', }; my $server = Contdmon->new ({}, $args); $server->Bind();
Run client.pl on your client machine, webserver(B), and it will send a request via the TCP protocol to your daemon waiting on server A.use strict; use IO::Socket; $| = 1; # flush output buffers immediately my ( $host ) = 'server'; my ( $port ) = 1234; # create the socket my $sock = IO::Socket::INET->new ( Proto => 'tcp', Type => SOCK_STREAM, PeerAddr => $host, PeerPort => $port ) or die "Cannot create socket : $!"; # so output goes to daemon immediately $sock->autoflush (1); print $sock "What is the time, Mr Wolf?\n";
|
|---|