Hi, I have two scripts: server.pl and client.pl. This is source code: server.pl:
#!/usr/bin/perl # serverfork.pl - a server that forks a child # process to handle client connections # use strict; use IO::Socket; use Sys::Hostname; use POSIX qw(:sys_wait_h); use Crypt::CBC; sub REAP { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; my $sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => 9000, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); $sock or die "no socket :$!"; # STDOUT->autoflush(1); my $cipher = Crypt::CBC->new( -key => '1234', -cipher => 'Blowfish' ); my($new_sock, $buf, $kid); while ($new_sock = $sock->accept()) { # execute a fork, if this is # the parent, its work is done, # go straight to continue next if $kid = fork; die "fork: $!" unless defined $kid; # child now... # close the server - not needed close $sock; while (defined($buf = <$new_sock>)) { chop $buf; server(); } exit; } continue { # parent closes the client since # it is not needed close $new_sock; } sub server { my $plaintext = $cipher->decrypt($buf); my $in = "SERVER: Text: ".$plaintext; my $ciphertext = $cipher->encrypt($in); print($new_sock $ciphertext); }
and client.pl:
#!/usr/bin/perl # use IO::Socket; use Crypt::CBC; my $sock = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => 9000, Proto => 'tcp'); $sock or die "no socket :$!"; my $cipher = Crypt::CBC->new( -key => '1234', -cipher => 'Blowfish' ); # clientfork.pl - a client that forks to # read from STDIN and write to a server use strict; use IO::Socket; my $host = shift || 'localhost'; my $port = shift || 9000; my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp'); $sock or die "no socket :$!"; $sock->autoflush(1); my($in, $buf, $kid); die "fork fail: $!" unless defined($kid = fork); if ($kid) { # parent reads from STDIN, prints to socket while (defined($in = <STDIN>)) { my $ciphertext = $cipher->encrypt($in); print $ciphertext; print $sock $ciphertext; } # kill the child process kill(TERM => $kid); } else { # child reads from socket, prints to STDOUT while (defined($buf = <$sock>)) { my $plaintext = $cipher->decrypt($buf); print $plaintext; } close $sock; }
Ok, when I run client.pl and I write anything, i have this output to SDOUT: http://wklej.org/id/124382/?zawin=0 In output from server.pl I have:
wmp@innocence:~/ISS/iss/daemon$ ./server.pl Ciphertext does not begin with a valid header for 'salt' header mode a +t ./server.pl line 48 wmp@innocence:~/ISS/iss/daemon$
What I cat do to fix this error? And, what I can do to send text in client only one enter click, no many enter click.

In reply to Crypt::CBC and IO:Socket by WMP

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.