Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* Perl IPC sockets
@ 1997-03-05  5:00 Milind Basole
  0 siblings, 0 replies; only message in thread
From: Milind Basole @ 1997-03-05  5:00 UTC (permalink / raw)



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();
}



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-03-05  5:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-03-05  5:00 Perl IPC sockets Milind Basole

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox