home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.madoka.org
/
2014.12.ftp.madoka.org.tar
/
ftp.madoka.org
/
pub
/
plum
/
2.x
/
plum2_15_4.lzh
/
support
/
pserver
< prev
next >
Wrap
Text File
|
1997-12-19
|
2KB
|
82 lines
#!/bin/perl -w
# $Id: pserver,v 2.4 1997/12/19 20:54:07 hasegawa Exp $
# copyright (c)1997 pupu_j <hasegawa@agusa.nuie.nagoya-u.ac.jp>
$SOCKADDR = 'S n a4 x8';
$PROTO = getprotobyname('tcp');
if ($] < 5) {
foreach $inc (@INC) {
if (-r "$inc/sys/socket.ph") {
eval 'require "sys/socket.ph"';
$SOCKET = "$inc/sys/socket.ph" unless $@;
last;
}
}
} else {
eval 'use Socket';
$SOCKET = 'Socket.pm' unless $@;
}
$AF_INET = eval { &AF_INET } || 2;
$PF_INET = eval { &PF_INET } || 2;
$SOCK_STREAM = eval { &SOCK_STREAM } || 1;
$SOMAXCONN = eval { &SOMAXCONN } || 16;
$INADDR_ANY = eval { &INADDR_ANY } || "\0\0\0\0";
$SOL_SOCKET = eval { &SOL_SOCKET};
$SO_REUSEADDR = eval { &SO_REUSEADDR };
$SO_KEEPALIVE = eval { &SO_KEEPALIVE };
&main(@ARGV);
sub main {
local(@args) = @_;
local($port, $rin, $rout, $serv, $stdin, $nf, $left, $cbuf, $sbuf, $tmp);
if (@args < 1) {
&usage();
exit(1);
}
($port) = @args;
socket(LISTEN, $PF_INET, $SOCK_STREAM, $PROTO) || die "cannot create socket\n";
if (defined($SOL_SOCKET)) {
setsockopt(LISTEN, $SOL_SOCKET, $SO_REUSEADDR, pack('l', 1)) if defined($SO_REUSEADDR);
setsockopt(LISTEN, $SOL_SOCKET, $SO_KEEPALIVE, pack('l', 1)) if defined($SO_KEEPALIVE);
}
bind(LISTEN, pack($SOCKADDR, $AF_INET, $port, $INADDR_ANY)) || die "cannot bind address\n";
listen(LISTEN, 1) || die "cannot listen\n";
accept(SOCKET, LISTEN);
$serv = fileno(SOCKET);
$stdin = fileno(STDIN);
$rin = '';
vec($rin, $serv, 1) = 1;
vec($rin, $stdin, 1) = 1;
select((select(SOCKET), $| = 1)[0]);
select((select(STDIN), $| = 1)[0]);
select((select(STDOUT), $| = 1)[0]);
$cbuf = $sbuf = $tmp = '';
for (;;) {
($nf, $left) = select($rout = $rin, undef, undef, undef);
if (vec($rout, $serv, 1)) {
sysread(SOCKET, $tmp, 1024) || die "connection closed\n";
$sbuf .= $tmp;
while ((($next, $rest) = split(/[\r\n]+/, $sbuf, 2)) == 2) {
print STDOUT $next, "\n";
$sbuf = $rest || '';
}
$sbuf = $next || '';
} elsif (vec($rout, $stdin, 1)) {
sysread(STDIN, $tmp, 1024) || die "connection closed\n";
$cbuf .= $tmp;
while ((($next, $rest) = split(/[\r\n]+/, $cbuf, 2)) == 2) {
print SOCKET $next, "\n";
$cbuf = $rest || '';
}
$cbuf = $next || '';
}
}
}
sub usage {
print 'usage: perl pserver <port>', "\n";
}