文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>sock

sock

时间:2009-04-01  来源:snowtty

定做的:
####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
相关阅读 更多 +
排行榜 更多 +
爱变幻智能清理

爱变幻智能清理

游戏工具 下载
MIYI泊松足球

MIYI泊松足球

浏览阅读 下载
樱桃骑士

樱桃骑士

动作格斗 下载