02/06/21 04:08 .net
とりあえずパールのソケット間通信クライアント。サーバーサイドは知らん
#tcp-client
( $them, $port ) = @ARGV;
$port = 2345 unless $port;
$them = 'localhost' unless $them;
$AF_INET = 2;
$SOCK_STREAM = 1;
$SIG{'INT'} = 'dokill';
sub dokill {
kill 9,$child if $child;
}
$sockaddr = 'S n a4 x8';
#chop($hostname = `変換`);
($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp')
unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
###IPを構造体からアドレスにする
@thisaddr = $thisaddr ; @thataddr = $thataddr ;
foreach (@thisaddr) {
@localip = unpack ('C4' , $_ );
print "localaddr = $localip[0] .$localip[1] .$localip[2] .$localip[3]\n";
}
foreach (@thataddr) {
@remoteip = unpack ('C4' , $_ );
print "$them = $remoteip[0] .$remoteip[1] .$remoteip[2] .$remoteip[3]\n";
}
$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);
if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
print "socket ok\n";
}
else {
die $!;
}
if (bind(S, $this)) {
print "bind local :ok\n";
}
else {
die $!;
}
if (connect(S,$that)) {
print "connect $them :ok\n";
}
else {
die $!;
}