From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,d04a86dd90b9ad13,start X-Google-Attributes: gidfee84,public X-Google-Thread: f678f,d04a86dd90b9ad13,start X-Google-Attributes: gidf678f,public From: Milind Basole Subject: Perl IPC sockets Date: 1997/03/05 Message-ID: <331D8A4F.4DF9@uiuc.edu>#1/1 X-Deja-AN: 223272881 Organization: University of Illinois at Urbana Reply-To: basole@uiuc.edu Newsgroups: comp.unix.osf.misc,comp.unix.osf.osf1 hello has anybody had problems running the perl IPC server available in Perl 5.002 man pages on OSF1 V4.0? i can't seem to get the server to communicate with clients (especially talking back to the client). any input would be appreciated. i do not have a lot of experience with sockets. the server in this message. thanks. milind basole basole@uiuc.edu ----------------------------------------------------------------- #!/usr/bin/perl -Tw require 5.002; use strict; BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } use Socket; use Carp; sub spawn; # forward declaration sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } my $port = shift || 2345; my $proto = getprotobyname('tcp'); socket(SERVER, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) || die "setsockopt: $!"; bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(SERVER,5) || die "listen: $!"; logmsg "server started on port $port"; my $waitedpid = 0; my $paddr; sub REAPER { $SIG{CHLD} = \&REAPER; # loathe sysV $waitedpid = wait; logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER; for ( $waitedpid = 0; ($paddr = accept(CLIENT,SERVER)) || $waitedpid; $waitedpid = 0, close CLIENT) { next if $waitedpid; my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; print "Hello there, $name, it's now ", scalar localtime, "\n"; spawn sub { print "Hello there, $name, it's now ", scalar localtime, "\n"; }; } sub spawn { my $coderef = shift; unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') { confess "usage: spawn CODEREF"; } my $pid; if (!defined($pid = fork)) { logmsg "cannot fork: $!"; return; } elsif ($pid) { logmsg "begat $pid"; return; # i'm the parent } # else i'm the child -- go spawn open(STDIN, "<&CLIENT") || die "can't dup client to stdin"; open(STDOUT, ">&CLIENT") || die "can't dup client to stdout"; ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr"; exit &$coderef(); }