Last active
November 11, 2015 02:45
-
-
Save adokoy001/9340448db83672dce1e0 to your computer and use it in GitHub Desktop.
prefork ssl echo server and client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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