in reply to Re^2: Have trouble implementing a shell-alike script
in thread Have trouble implementing a shell-alike script

Thank you for making my point. If you click on your own link, you will see the below.
A common misconception in socket programming is that \n eq \012 everywhere. When using protocols such as common Internet protocols, \012 and \015 are called for specifically, and the values of the logical \n and \r (carriage return) are not reliable.
The link gives examples:
print SOCKET "Hi there, client!\r\n"; # WRONG print SOCKET "Hi there, client!\015\012"; # RIGHT <- also wrong!
The \r is wrong. I think that:
print SOCKET "Hi there, client!\n"; #WORKS, on Unix or Windows!! #sends CR/LF: \015\012
Basically do not ever put \r (CR) in a print statement. I agree.
For the \n in the source code, Perl will send either LF or CRLF depending upon what it is taking to.

Replies are listed 'Best First'.
Re^4: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 10:09 UTC

    Thank you for making my point. If you click on your own link, you will see the below.

    And incredibly, in the line right after the one you quoted it says

    print SOCKET "Hi there, client!\r\n"; # WRONG print SOCKET "Hi there, client!\015\012"; # RIGHT

    Keep reading smartalec

      Before this escalates, please step back, make your point with code and data, and don't resort to names. They do nothing to prove your point.

      --MidLifeXis

        Before this escalates, please step back, make your point with code and data, and don't resort to names. They do nothing to prove your point. --MidLifeXis

        This is already over :) Pay close attention to the timestamps

        The documentation made my point, but no, wasn't enough for Marshall, esp coming from Anonymous Monk

        Marshall event went so far as to update his post without notice trying to justify his dismissive response and failing badly

        He couldn't admit being wrong, didn't like being corrected, esp by Anonymous Monk

        Smartalec was generous but appropriate and civil

        There isn't enough time in the day to check every post to make sure someone isn't wrong on the internet, so I should have used harsher language

        Your post is worse than his, you're just fanning the flames because you're just prejudiced against me

        You're wrong, so here is some harsher language for you :P

        You're worse than a can't-admit-when-he-is-wrong-dont-like-to-be-corrected-by-documentation-smartalec

        You're a calm-down-everybody-in-an-empty-room-chicken-little-flame-fanner

      So anonymous smartalec, post some code that proves your point.

      I think that it is beyond discussion that Perl prints LF for \n on a Unix system and Perl prints CRLF for \n on a Windows system. I further claim that Perl will print CRLF for \n on a Network socket.

        So anonymous smartalec, post some code that proves your point.

        The documentation proves my point. You want to refute the documentation, you post the code.

        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

        I think that it is beyond discussion that Perl prints LF for \n on a Unix system and Perl prints CRLF for \n on a Windows system.

        No. That depends entirely upon layers/discipline and not on platforms.

        Yes, some platforms have defaults, but they're not absolute.

Re^4: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 11:00 UTC