Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Using select() on only one socket

by GrandFather (Saint)
on Sep 04, 2005 at 10:07 UTC ( #489036=note: print w/replies, xml ) Need Help??

in reply to Re^2: Using select() on only one socket
in thread Using select() on only one socket

The Camel has the following to say (much better than I could!):

A common misconception in socket programming is that \n will be \012 everywhere. In many common Internet protocols, \012 and \015 are specified, and the values of Perl's \n and \r are not reliable since they vary from system to system:

print SOCKET "Hi there, client!\015\012"; # right print SOCKET "Hi there, client!\r\n"; # wrong

However, using \015\012 (or \cM\cJ, or \x0D\x0A, or even v13.10) can be tedious and unsightly, as well as confusing to those maintaining the code. The Socket module supplies some Right Things for those who want them:

use Socket qw(:DEFAULT :crlf); print SOCKET "Hi there, client!$CRLF" # right

When reading from a socket, remember that the default input record separator $/ is \n, which means you have to do some extra work if you're not sure what you'll be seeing across the socket. Robust socket code should recognize either \012 or \015\012 as end of line:

use Socket qw(:DEFAULT :crlf); local ($/) = LF; # not needed if $/ is already \012 while (<SOCKET>) { s/$CR?$LF/\n/; # replace LF or CRLF with logical newline }

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^4: Using select() on only one socket
by ivanatora (Sexton) on Sep 04, 2005 at 10:17 UTC
    I'm using IO::Socket, not Socket... and in my case \n is \n ;)If I just do
    while (<$sock>){ print; }
    I see everything printed normaly with line separated by '\n'.
    If I send lines terminated by '\n', the server responds normaly. I don't think the problem is right here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://489036]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-01-25 01:54 GMT
Find Nodes?
    Voting Booth?