-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcpclient.pl
executable file
·72 lines (62 loc) · 1.36 KB
/
tcpclient.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
#!/usr/local/bin/perl
# client.pl
# by Yoshioka Tsuneo([email protected])
# Copy,Edit,Distribute FREE!
#
use Socket;
use POSIX;
# login server
# return value:0:OK, -1:NO
sub tcp_setup_client
{
local(*SH,$host,$port)=@_;
local($iaddr,$paddr,$proto);
#print "(host=$host,port=$port)\n";
$proto=getprotobyname('tcp') || die "Error getprotobyname:$!";
if(!socket(SH,PF_INET,SOCK_STREAM,$proto)){
print STDERR "Error socket():$!\n";
return -1;
}
if(!($iaddr = inet_aton($host))){
print STDERR "Error inet_aton($host)\n";
return -1;
}
if ( !($port =~ /^\d+$/) && !($port = getservbyname($port,'tcp'))){
print STDERR "Error getservbyname($port):$!\n";
return -1;
}
$paddr=sockaddr_in($port,$iaddr);
#print "(port=$port,iaddr=",inet_ntoa($iaddr),")\n";
if(!connect(SH,$paddr)){
print STDERR "Error connect(host=$host,port=$port):$!\n";
return -1;
}
return 0;
}
sub usage
{
print "usage: $0 <server name> <port number>\n";
}
#------------main-----------------------------
if($0 eq __FILE__){
if($#ARGV!=1){
&usage();
exit 1;
}
local($server)=shift;
local($port)=shift;
local(*S);
$err=&tcp_setup_client(*S,$server,$port);
if($err==-1){
print $err_msg,"\n";
exit 1;
}
while(<STDIN>){
print S $_;
}
while(<S>){
print $_;
}
close(S);
}
1;