Skip to content

Instantly share code, notes, and snippets.

@ksurent
Created September 17, 2016 21:57
Show Gist options
  • Save ksurent/107acc2cf7c2193496b1a08a5a951ba2 to your computer and use it in GitHub Desktop.
Save ksurent/107acc2cf7c2193496b1a08a5a951ba2 to your computer and use it in GitHub Desktop.
use v5.14; [6/1836]
use warnings;
use IO::Socket;
use POSIX qw(:sys_wait_h);
my($ip, $host) = @ARGV;
my $sock = IO::Socket::INET->new(
PeerAddr => "$ip:80",
Proto => "tcp",
) or die($!);
say "Parent: connected";
my $netstat = "netstat -n | grep $ip || echo nothing";
if(my $pid = fork()) {
my $terminated = 0;
$SIG{CHLD} = sub {
my $pid2 = waitpid(-1, WNOHANG);
if($pid == $pid2) {
$terminated = 1;
}
};
$sock->send(join(
"\r\n",
"GET / HTTP/1.1",
"Host: $host",
"Connection: close",
"\r\n"
));
$sock->recv(my $buf, 42);
say "Parent: received ${\length $buf} bytes";
$sock->close;
say "Parent: close()";
1 while not $terminated;
}
else {
sleep(3);
say "Child: before close()";
system($netstat);
say "\n<Enter> to close() in the child";
<>;
$sock->close;
say "Child: after close()";
system($netstat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment