#!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;