Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

research for secure tchat client server use IO::Socket::SSL

by swilting (Beadle)
on Feb 15, 2012 at 19:34 UTC ( #954050=perlquestion: print w/ replies, xml ) Need Help??
swilting has asked for the wisdom of the Perl Monks concerning the following question:

hi guys hi master of "Fu" any update

I am doing some research on the server and client for the chat. I have long taken the trouble to consult the documentation that is different and both PerlMonks different pages that deal with this vast subject. I try to find a minimal example because I want to turn into non securise securise. here is the status of my research

#!/usr/bin/perl use warnings; use strict; use IO::Socket::SSL 'inet4'; use threads; use threads::shared; $|++; print "$$ Server started\n";; # do a "top -p -H $$" to monitor server ++ threads our @clients : shared; @clients = (); my $server = new IO::Socket::SSL( Timeout => 7200, Proto => "tcp", LocalPort => 42000, Reuse => 1, Listen => 3, SSL_use_cert => 1, SSL_verify_mode => 0x00, SSL_key_file => '/home/swilting/perltest/private/localhost.key' +, SSL_cert_file => '/home/swilting/perltest/certs/localhost.cert', SSL_passwd_cb => sub { return "" }, ); my $num_of_client = -1; while (1) { my $client; do { $client = $server->accept; } until ( defined($client) ); my $peerhost = $client->peerhost(); print "accepted a client $client, $peerhost, id = ", ++$num_of_cli +ent, "\n"; my $fileno = fileno $client; push (@clients, $fileno); #spawn a thread here for each client my $thr = threads->new( \&processit, $client, $fileno, $peerhost ) +->detach(); } # end of main thread sub processit { my ($lclient,$lfileno,$lpeer) = @_; #local client if($lclient->connected){ # Here you can do your stuff # I use have the server talk to the client # via print $client and while(<$lclient>) print $lclient "$lpeer->Welcome to server\n"; while(<$lclient>){ # print $lclient "$lpeer->$_\n"; print "clients-> @clients\n"; foreach my $fn (@clients) { open my $fh, ">&=$fn" or warn $! and die; print $fh "$lpeer->$_" } } } #close filehandle before detached thread dies out close( $lclient); #remove multi-echo-clients from echo list @clients = grep {$_ !~ $lfileno} @clients; } __END__
#!/usr/bin/perl use warnings; use strict; use Tk; use IO::Socket::SSL 'inet4'; require Tk::ROText; #get id my $name = shift || 'anon'; # create the socket my $host = 'localhost'; my $port = 42000; my $socket = IO::Socket::SSL->new( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', SSL_use_cert => 1, SSL_verify_mode => 0x00, SSL_key_file => '/home/swilting/perltest/private/localhost.k +ey', SSL_cert_file => '/home/swilting/perltest/certs/localhost.cer +t', SSL_passwd_cb => sub { return "" }, ); defined $socket or die "ERROR: Can't connect to port $port on $host: $ +!\n"; print STDERR "Connected to server ...\n"; my $mw = new MainWindow; my $log = $mw->Scrolled('ROText', -scrollbars=>'ose', -height=> 5, -width=>45, -background => 'lightyellow', )->pack; my $txt = $mw->Entry( -background=>'white', )->pack(-fill=> 'x', -pady=> 5); $mw ->bind('<Any-Enter>' => sub { $txt->Tk::focus }); $txt->bind('<Return>' => [\&broadcast, $socket]); $mw ->fileevent($socket, readable => sub { my $line = <$socket>; unless (defined $line) { $mw->fileevent($socket => readable => ''); return; } $log->insert(end => $line); $log->see('end'); }); MainLoop; sub broadcast { my ($ent, $sock) = @_; my $text = $ent->get; $ent->delete(qw/0 end/); print $sock $name.'->'. $text, "\n"; } __END__
#Server.pl #!/usr/local/bin/perl use strict; use warnings; use IO::Socket::SSL 'inet4'; my $server = IO::Socket::SSL->new( LocalPort=> 42000, ## Type=>SOCK_STREAM, Reuse=>1, Listen=>5, Proto => 'tcp', SSL_use_cert => 1, SSL_verify_mode => 0x00, SSL_key_file => '/home/swilting/perltest/private/localhost.key', SSL_cert_file => '/home/swilting/perltest/certs/localhost.cert', SSL_passwd_cb => sub { return "" }, )or die ("Could not create server"); while(my $client=$server->accept()){ my $child_pid; unless(defined ($child_pid=fork())){die"can not fork\n";} if(defined($child_pid)){ while(my $line = <$client>){ print "CLIENT says:$line\n"; } }else{ while(my $line = <>){ print $client "$line\n"; } } } redo; close($server); <>;

the server running the threads fails with a segmentation fault and my client fails after two requests I do not know why and characters displayed on the client encodes are poorly I do not know either why

Comment on research for secure tchat client server use IO::Socket::SSL
Select or Download Code
Re: research for secure tchat client server use IO::Socket::SSL
by Corion (Pope) on Feb 15, 2012 at 21:36 UTC
    use IO::Socket::SSL 'inet4'; use threads;

    Looking at IO::Socket::SSL, it says:

    IO::Socket::SSL is not threadsafe. This is because IO::Socket::SSL is based on Net::SSLeay ...

    So, you'll have to find another approach.

      Net-SSLeay-1.43 is thread-safe

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://954050]
Approved by petdance
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2015-07-06 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls