in reply to Daemon with Net::Daemon
For the client side, you can do something like:#!/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"; } }
#!/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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Daemon with Net::Daemon
by BUU (Prior) on Sep 02, 2003 at 16:42 UTC | |
by Preceptor (Deacon) on Sep 03, 2003 at 13:06 UTC |