#!/usr/bin/perl
use strict;
use Socket; #导入Socket库
use Sys::Hostname;
my $file_head_length = 24;
my $packet_head_length = 16 ;#UDP
#print("$#ARGV\n");
if($#ARGV != 2)
{
print("input net_data_send.pl net_data_file.cap ip port\n");
}
open NETDATA,"<$ARGV[0]" or die "$!";
#my ($type,$version,$fileLen,$RECT,$frameRate,$frameCount) = unpack("a3C1I1a9S1S1",$file_hdr);
my $host = $ARGV[1]; #第一参数为主机变量
my $port = $ARGV[2]; #第二参数为端口变量
my $iaddr = gethostbyname(hostname());
my $proto = getprotobyname('udp');
my $packhost= inet_aton($host); #压缩主机地址
my $address = sockaddr_in($port,$packhost); #压为sockaddr_in模式
socket(SOCKET,AF_INET,SOCK_DGRAM,$proto) || die "socket: $!"; #建立UDP套接字
my $paddr = sockaddr_in(0, $iaddr); # 0 means let kernel pick
bind(SOCKET, $paddr) || die "bind: $!";
my $total = 0;
my $packet;
$SIG{INT}=\&my_int;
binmode(NETDATA);########this is very important when you read file as binary#######
#while(1)
#{
my $file_hdr;
# seek(NETDATA,0,0);
read(NETDATA, $file_hdr, $file_head_length);
my $cur_pos = $file_head_length;
while(read(NETDATA, $packet, $packet_head_length ))
{
my($unknown1,$unknown2,$size1,$size2) = unpack("I4",$packet);
#print("$size1, $cur_pos\n");
$cur_pos += $packet_head_length + $size2;
#seek(NETDATA,$cur_pos,0);
read(NETDATA, $packet, $size2) || die "$!";
my($packet1, $rawdata) = unpack("c42c*",$packet);
$total += 1;
#if($total == 1000)
#{
# select(undef, undef, undef, 1);#millsec sleep
#}
defined(send(SOCKET,$rawdata,0,$address)) || die "send $host: $!"; #向套接字发送字符串变量
}
#}
print("sum : $total\n");
sub my_int{
print("sum : $total\n");
exit;
}
|