定做的:
####client.pl ###
#!/usr/bin/perl
use Socket;
@remote_ip=(192,168,3,15);
$remote_port=8081;
$local_port=8080;
@local_ip=(192,168,3,15);
$AF_INET=2;
$STREAM=1;
$PROTOCOL=0;
$svr_addr=pack('S n C4 x8',$AF_INET,$remote_port,@remote_ip);
$client_addr=pack('S n C4 x8',$AF_INET,$local_port,@local_ip);
$SIG{'INT'}=dokill();
sub dokill() {
kill 9,$child if $child;
}
socket(SVR,$AF_INET,$STREAM,$PROTOCOL) || die "Cannot make socket:$! ";
bind(SVR,$client_addr) || die "cannot bind:$! ";
if (connect(SVR,$svr_addr))
{
print "Connect succeeded. ";
}
else {
die "connect failed:$! ";
}
select(SVR);
$|=1;
select(STDOUT);
if ($child=fork())
{
# this is the parent!
while()
{
print SVR "$_";
}
sleep(3);
&dokill();
} else {
# this is the child
while()
{
print "Server:$_";
}
}
close(SVR);
###srv.pl ###
#!/usr/bin/perl
#use Socket;
$AF_INET=2;
$STREAM=1;
$PROTOCOL=0;
$PORT=8081;
@IP=(192,168,3,15);
$my_ip=pack(C4,@ip);
socket(SVR,$AF_INET,$STREAM,$PROTOCOL) || die "Cannot make socket $! ";
$my_addr=pack('S n C4 x8',$AF_INET,$PORT,@IP);
bind(SVR,$my_addr) || die "cannot bind $! ";
select(SVR);
$|=1;
select(STDOUT);
listen(SVR,5) || die "cannot listem $! ";
$i=0;
for ($con=1;;$con++)
{
print "I am waiting for connection $con ";
# $i++;
($client_addr=accept(NS,SVR)) || die "cannot accept ";
select(NS);
$|=1;
select(STDOUT);
if (($child=fork())==0)
{
# this is a child process!
($af,$port,$inetaddr)=unpack('S n C4 x8',$client_addr);
while()
{
print "Received from client:$_";
open(FH,">txt");
print FH "$_";
close(FH);
print NS "Server $con:$_";
}
close(NS);
print "Client went away.Forked server $con exiting... ";
exit;
}
else {
# this is the parent!
# all to do is close(NS);
close(NS);
}
}
close(SVR);
ponderh
阅读(1291) | 评论(0) | 转发(0) |