-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcpmclient.pl
executable file
·100 lines (96 loc) · 2.34 KB
/
tcpmclient.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/local/bin/perl
use Socket;
use Time::HiRes qw(gettimeofday tv_interval);
use Fcntl;
use POSIX;
$SIG{'PIPE'} = sub{print "SIGPIPE happened\n";};
sub usage
{
print "usage: tcpmclient.pl -H<server_host> -P<server_port> -C<connection number> -S<allsize>\n";
}
if($0 eq __FILE__){
$port = 12345;
$host = "localhost";
$allsize = 100000;
$packetsize = 1000;
$conn = 5;
while($_=$ARGV[0]){
if(/^-P/){$port=$';}
elsif(/^-H/){$host=$';}
elsif(/^-C/){$conn=$';}
elsif(/^-S/){$allsize=$';}
else{print "syntax error\n";}
shift;
}
$ti = &tcpmclient($port,$host,$conn,$allsize);
print "Time = $ti\n";
}
sub tcpmclient
{
local($port,$host,$conn,$allsize) = @_;
local($allsendsize) = 0;
local(%sendsize);
$onesize = int($allsize / $conn);
$SH_BASE = "SH00000";
$t0 = [gettimeofday];
for($i=0;$i<$conn;$i++){
$SH = $SH_BASE++;
socket($SH,PF_INET,SOCK_STREAM,0)|| die "can't open socket:$!";
fcntl($SH,F_SETFL,O_NONBLOCK) || die "fcntl:$!";
inet_aton($host) || die "sockaddr_in:$!";
$ret = connect($SH,sockaddr_in($port,inet_aton($host)));
if((! $ret) && $! != EINPROGRESS){
die "can't connect:$!";
}
# select((select(SH),$|=1)[0]);
$STATE{$SH} = "WRITE";
$sendsize{$SH} = 0;
}
while(%STATE){
$win = '';
while(($SH,$state) = each(%STATE)){
vec($win,fileno($SH),1) = 1;
}
print "Selecting\n";
$n = select(undef,$wout=$win,undef,undef);
print "Selected(n=$n)\n";
if($n<=0){die "select:$!";}
%hash = %STATE;
while(($SH,$state) = each(%hash)){
if(vec($wout,fileno($SH),1)==1){
if($state eq "WRITE"){
$buf = ("0" x ($packetsize-1)) . "\n";
$n = send($SH,$buf,0);
if($n<=0 && $! != EWOULDBLOCK){
die "[$SH] send error(n=$n):$!";
}
if($n>0){
$sendsize{$SH} += $packetsize;
$allsendsize += $packetsize;
$ti = tv_interval($t0);
print "($ti)[$SH] $sendsize{$SH}/$onesize , $allsendsize/$allsize sended\n";
if($sendsize{$SH} >= $onesize){
close($SH);
delete $STATE{$SH};
$ti = tv_interval($t0);
print "($ti)[$SH] Close\n";
}
if($allsendsize>=$allsize){
while(($s,undef)=each %STATE){
close($s);
delete $STATE{$s};
$ti = tv_interval($t0);
print "($ti)[$s] Close\n";
}
last;
}
}
}
}
}
}
# sleep(10000);
$ti = tv_interval($t0);
return $ti;
}
1;