This was discussed quite recently in node 286340. You may find some good answer's there.

Looking at what you are saying, server B needs to be able to run commands on server A. You may find that something like rsh, ssh is the tool for the job. However, if it's something like 'server B connects, get's result of a defined commmand' then you're fairly safe in perl.
If you have some way for the webserver to send the command it requires to server A, then consider very carefully security implications - a daemon that just accepts remote commands and runs them can be an extremely dangerous thing.
In order to create the connection, then the module you want may be Net::Telnet. Or you could do it the slightly harder way by using IO::Socket (but you'll learn more)

IMHO forking 'just because it needs to be parallel' is ugly, and the wrong answer. A 'fork', should be used in situations that you need to create an identical copy of the process. In most daemon examples I've seen, you don't.

I know the perl cookbook recommends doing it this way, but I don't agree. I think the right way to do it (at least with a simple sort of 'do stuff, return result, disconnect' type daemon) is to use IO::Select, thus:

#!/bin/perl use strict; use warnings; use IO::Select; use IO::Socket; #initialise our socket my $listener = IO::Socket::INET -> new( LocalPort => 5001, Proto => "tcp", Reuse => 1, Listen => 20, Blocking => 0, ); $listener -> autoflush(1); #create our 'select' groups my $listen_select = new IO::Select( $listener ); my $sockets = new IO::Select ( ); #A timeout of 0 can be workable, but is inefficient unless #you have _lots_ of IO my $timeout = 1; #seconds; while () { if ( $sockets -> count ) { #check for an incoming socket. Could probably merge #the two can_read statements. if ( $listen_select -> can_read($timeout) ) { my $client; my $raddr = accept($client, $listener); $sockets -> add ( $client ); $client -> autoflush(1); $timeout = 0; print "Connection accepted from ", print unpack ( "C*", $raddr ) +, "\n"; } my @ready = $sockets -> can_read($timeout); #if any socket has pending data, read it and act on it. foreach my $handle ( @ready ) { my $read_tmp = <$handle>; if ( !defined ( $read_tmp ) ) { print "Filehandle closed.\n"; $sockets -> remove ( $handle ); $handle -> close; } else { #do processing here. #imagine doing something good with read_tmp print "Got data: $read_tmp\n"; #and sending something cool to the client print $handle "Fwatoom\n"; } } } #if count else { #we have no handles in our thingy, so we sit idle waiting for #a connection. print "No pending data. Idling for connect()\n"; my $client; my $raddr = accept($client, $listener); $sockets -> add ( $client ); $client -> autoflush(1); $timeout = 0; print "Connection accepted from ", print unpack ( "C*", $raddr ), +"\n"; } }
For the client side, you can do something like:
Note that the two won't _quite_ work together - you'll have to modify one or the other. The server expects an input, and then sends an output, this snippet will simply get all the data it can from the server, and then exit when the connection closes.
#!/bin/perl use strict; use warnings; use IO::Socket; my $destination_port = 5001; #change these how you will my $destination_address = "localhost"; my $socket = new IO::Socket::INET ( proto => "tcp", PeerAddr => $destination_address, PeerPort => $destination_port, ) or die "$!"; #send our "command" to the server print $socket "hello\n"; #See what the response is. In this case, we'll just keep #reading until the socket is closed remotely. while ( <$socket> ) { print; }

In reply to Re: Daemon with Net::Daemon by Preceptor
in thread Daemon with Net::Daemon by tja_ariani

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.