J
janicehwang1325
hi,
I post the program before, however, based on the response, i simplify
my server code in the following. I still got error when the clients on
the same LAN trying to connect to the server it gives me segmentation
fault error. What is the cause? For your information, the prefork
server runs perfectly well without any errors. However, due to
specification, i need to design my server handling multiple clients
using threads.
--------------------------------------------------------------------------------------------------------------------------------------------
use threads;
#!/usr/bin/perl
use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use Time::Local;
use DBI;
my ($sock, $s, $v_mode);
unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
($port) = @ARGV;
# Check to make sure that we were not accidentally run in the wrong
# directory:
unless (-d "certs") {
if (-d "../certs") {
chdir "..";
} else {
die "No CA Certs\n";
}
}
if(!($sock = IO::Socket::SSL->new( Listen => 20,
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1,
SSL_verify_mode => 0x01,
SSL_use_cert => 1,
SSL_passwd_cb => sub {return
"haha"},
SSL_key_file =>
'certs/server_key.pem',
SSL_cert_file =>
'certs/server_certs.pem',
)) ) {
warn "unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
exit(0);
}
warn "SSL socket created: $sock.\n";
warn "waiting for next connection......\n";
sub handle_connection{
$s = shift;
$output = shift || $s;
$exit = 0;
#$s->autoflush(1);
my ($peer_cert, $subject_name, $issuer_name, $date, $str);
if( ! $s ) {
warn "error: ", $s->errstr, "\n";
$exit = 1;
break;
}
chomp($_);
while(<$s>){
if( ref($s) eq "IO::Socket::SSL" && $s->connected()) {
$subject_name =
$s->peer_certificate("subject");
$issuer_name =
$s->peer_certificate("issuer");
print "from client > $_";
if(/equal/i){
#print $s "No new coming log...\n";
last;
}
}
else{
$exit = 1;
}
last if ($exit == 1);
}
print "Thread exited lor..\n";
}
while(1){
while(($s = $sock->accept())) {
#$s->autoflush(1);
print "new thread here .. \n";
#$thread = threads->create(\&handle_connection, $s);
#$thread->detach;
async(\&handle_connection, $s)->detach;
}
}
I post the program before, however, based on the response, i simplify
my server code in the following. I still got error when the clients on
the same LAN trying to connect to the server it gives me segmentation
fault error. What is the cause? For your information, the prefork
server runs perfectly well without any errors. However, due to
specification, i need to design my server handling multiple clients
using threads.
--------------------------------------------------------------------------------------------------------------------------------------------
use threads;
#!/usr/bin/perl
use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use Time::Local;
use DBI;
my ($sock, $s, $v_mode);
unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
($port) = @ARGV;
# Check to make sure that we were not accidentally run in the wrong
# directory:
unless (-d "certs") {
if (-d "../certs") {
chdir "..";
} else {
die "No CA Certs\n";
}
}
if(!($sock = IO::Socket::SSL->new( Listen => 20,
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1,
SSL_verify_mode => 0x01,
SSL_use_cert => 1,
SSL_passwd_cb => sub {return
"haha"},
SSL_key_file =>
'certs/server_key.pem',
SSL_cert_file =>
'certs/server_certs.pem',
)) ) {
warn "unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
exit(0);
}
warn "SSL socket created: $sock.\n";
warn "waiting for next connection......\n";
sub handle_connection{
$s = shift;
$output = shift || $s;
$exit = 0;
#$s->autoflush(1);
my ($peer_cert, $subject_name, $issuer_name, $date, $str);
if( ! $s ) {
warn "error: ", $s->errstr, "\n";
$exit = 1;
break;
}
chomp($_);
while(<$s>){
if( ref($s) eq "IO::Socket::SSL" && $s->connected()) {
$subject_name =
$s->peer_certificate("subject");
$issuer_name =
$s->peer_certificate("issuer");
print "from client > $_";
if(/equal/i){
#print $s "No new coming log...\n";
last;
}
}
else{
$exit = 1;
}
last if ($exit == 1);
}
print "Thread exited lor..\n";
}
while(1){
while(($s = $sock->accept())) {
#$s->autoflush(1);
print "new thread here .. \n";
#$thread = threads->create(\&handle_connection, $s);
#$thread->detach;
async(\&handle_connection, $s)->detach;
}
}