Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Single-machine IPC via sockets and other things to say in the dark

by BastardOperator (Monk)
on Sep 14, 2000 at 01:25 UTC ( [id://32367]=note: print w/replies, xml ) Need Help??


in reply to "Single-machine IPC via sockets" and other things to say in the dark

This uses Unix domain sockets, it's from the perlipc manpage. you didn't specify what you were looking for
#!/usr/bin/perl -w # The client use Socket; use strict; my ($socky, $line); $socky = shift || '/tmp/mysock'; socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "socket: $!"; connect(SOCK, sockaddr_un($socky)) || die "connect: $!"; while (defined($line = <SOCK>)) { print $line; } exit; ################# #!/usr/bin/perl -wT # The server use Socket; use strict; use Carp; BEGIN { $ENV{PATH} = '/usr/ucb:/bin'; } sub logit { print "$0 $$: @_ at ", scalar localtime, "\n"; } my $NAME = '/tmp/mysock'; my $uaddr = sockaddr_un($NAME); my $proto = getprotobyname('tcp'); socket(Server,PF_UNIX,SOCK_STREAM,0) || die "socket: $!"; unlink($NAME); bind (Server, $uaddr) || die "bind: $!"; listen(Server,SOMAXCONN) || die "listen: $!"; logit "server started on $NAME"; my $waitedpid; sub REAPER { $waitedpid = wait; $SIG{CHLD} = \&REAPER; logit "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER; for ($waitedpid = 0; accept(Client,Server) || $waitedpid; $waitedpid = 0, close Client) { next if $waitedpid; logit "connection on $NAME"; spawn sub { print "Hello there, it's now ", scalar localtime, "\n"; exec '/usr/games/fortune' or die "can't exec fortune: +$!"; }; }
  • Comment on Re: Single-machine IPC via sockets and other things to say in the dark
  • Download Code

Replies are listed 'Best First'.
RE: Re: Single-machine IPC via sockets and other things to say in the dark
by merlyn (Sage) on Sep 14, 2000 at 01:33 UTC
    Ahh, this uses the ugly %SIG-handler code that breaks servers sooner or later. There are better ways to reap kids. I have a few samples in my columns. Best look for examples that use a no-wait waitpid at strategic points in the top-level loop.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://32367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found