Perl Socket Server using upack

#!usr/bin/perl
#服务端 
use IO::Handle; 
use Socket; 
use checkfax;
use sendFax;

$port=6010; 
$host='28.17.250.11'; 
$packhost=inet_aton($host); 
$address=sockaddr_in($port,$packhost); 
socket(SERVER,AF_INET,SOCK_STREAM,getprotobyname('tcp'))|| die "socket create:$!\n"; 



#setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 2) || die "socket reuse: $!\n";
bind(SERVER,$address) || die "socket bind:$!\n"; 
listen(SERVER,2)||die "socket listen:$!\n"; 
my $receive=0;

#binmode(FILENEW);
while(1){
sleep 2;

next unless (accept(CLIENT,SERVER)); 
CLIENT->autoflush(1); 
#判断传真是否可用
my $tom = checkfax->new();
my $result = $tom->checkFax();
warn "result:".$result."\n";
if ($result eq 'active'){
#如果传真忙 就返回字母d
 syswrite(CLIENT,"d\n");

}
else{
#否则删除已存在文件
my $file = "/root/myhen.tif";
if(-e $file){
unlink $file;
}
system(`touch $file`);
open (FILENEW,">$file") or die "can not open myhen.tif";

#读取8个字节
sysread(CLIENT,$front,8);
#利用unpack进行解码
my ($id,$phonelength)=unpack("i2",$front);

sysread(CLIENT,$front1,$phonelength);
my $phone = unpack("A$phonelength",$front1);

sysread(CLIENT,$front2,4);
my $filelength=unpack('i',$front2);


print "id:".$id."phonelength:".$phonelength."phone:".$phone."filelength:".$filelength."\n";
my $count=0;
#每次读取1024循环写入文件
 while ($count<$filelength)
    {
      $read = sysread(CLIENT,$buf,1024);
#把读取的字节写入文件
	syswrite(FILENEW,$buf,1024,0);
	$count+=$read; 	
#	warn $count."\n";
	#last if($read == 0);
}
#warn $count;

close FILENEW;
my $sendfax = sendFax->new($phone,$id);
$sendfax->sendFax();
syswrite(CLIENT,"c\n");
 
print '666';


}
}
close CLIENT;
close SERVER; 
exit 1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值