Here it is, adopted from perlipc, which has small typo ( die ... unless if... )

The server

#!/usr/bin/perl -Tw use strict; BEGIN { $ENV{PATH} = "/usr/bin:/bin" } use Socket; use Carp; my $EOL = "\015\012"; sub logmsg { print "$0 $$: @_ at ", scalar localtime(), "\n" } my $port = shift || 2345; die "invalid port" unless $port =~ /^ \d+ $/x; my $proto = getprotobyname("tcp"); socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $! +"; bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(Server, SOMAXCONN) || die "listen: $!"; logmsg "server started on port $port"; my $paddr; $SIG{CHLD} = \&REAPER; for ( ; $paddr = accept(Client, Server); close Client) { my($port, $iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr, AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; print Client prelen( "Hello there, $name, it's now ", scalar local +time(), $EOL ); print Client prelen( 'backslash \n__'."\n" ); print Client prelen( 'backslash \r\n'."\r\n" ); print Client prelen( 'backslash EOL_'.$EOL ); } sub prelen { my $str = join '',@_; my $len = length $str; return "($len+6)$str"; }

And the client

#!/usr/bin/perl -w use strict; use Socket; my ($remote, $port, $iaddr, $paddr, $proto, $line); $remote = shift || "localhost"; $port = shift || 2345; # random port if ($port =~ /\D/) { $port = getservbyname($port, "tcp") } die "No port" unless $port; $iaddr = inet_aton($remote) || die "no host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname("tcp"); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect(SOCK, $paddr) || die "connect: $!"; binmode SOCK or die "binmode $!"; ## no effect on client side while ($line = <SOCK>) { my $length = length $line; print "($length)$line"; } close (SOCK) || die "close: $!"; exit(0); __END__ $ perl client.pl (65)(59+6)Hello there, localhost, it's now Wed Sep 14 03:53:10 2011 (21)(15+6)backslash \n__ (22)(16+6)backslash \r\n (22)(16+6)backslash EOL_

The output from client

(65)(59+6)Hello there, localhost, it's now Wed Sep 14 03:53:10 2011
(21)(15+6)backslash \n__
(22)(16+6)backslash \r\n
(22)(16+6)backslash EOL_

The odd man out is the lone \n, it doesn't become \r\n

The strings are all the same size except for the line endings, with \n remaining \012 and \r remaining \015

So the documentation is correct


In reply to Re^6: Have trouble implementing a shell-alike script by Anonymous Monk
in thread Have trouble implementing a shell-alike script by PerlOnTheWay

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.