Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5376432
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-04-01 23:34:13

定做的:
####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) |
0

上一篇:WWW::Mechanize之后小结

下一篇:pure perl tail

给主人留下些什么吧!~~