Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to research for secure tchat client server use IO::Socket::SSL by swilting

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2015-07-06 08:53 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 (70 votes), past polls