Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adokoy001
Last active November 11, 2015 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adokoy001/9340448db83672dce1e0 to your computer and use it in GitHub Desktop.
Save adokoy001/9340448db83672dce1e0 to your computer and use it in GitHub Desktop.
prefork ssl echo server and client
use strict;
use warnings;
use IO::Socket::SSL;
while(1){
my $c = IO::Socket::SSL->new(
PeerAddr => "localhost",
PeerPort => 5001,
Proto => "tcp",
SSL_verify_mode => 'SSL_VERIFY_NONE'
) or die $!;
my $time = time();
$c->print("hello : $time\n");
print $c->getline;
$c->close;
sleep(1);
}
use strict;
use warnings;
use Parallel::Prefork;
use IO::Socket::SSL;
sub MaxRequestsPerChild(){ 100 }
my $listen_sock = IO::Socket::SSL->new(
Listen => 5,
LocalAddr => '0.0.0.0:5001',
Proto => 'tcp',
SSL_cert_file => 'scrt.pem',
SSL_key_file => 'skey.pem'
) or die $!;
my $pm = Parallel::Prefork->new({
max_workers => 4,
trap_signals => {
TERM => 'TERM',
HUP => 'TERM'
}
});
while ($pm->signal_received ne 'TERM') {
$pm->start and next;
my $reqs_before_exit = MaxRequestsPerChild;
$SIG{TERM} = sub { $reqs_before_exit = 0 };
while ($reqs_before_exit-- > 0) {
my $s = $listen_sock->accept();
my $q = $s->getline;
print "Received: $q";
$s->print("You send: $q");
$s->close;
}
$pm->finish;
}
$pm->wait_all_children();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment