I am trying to work with a small Perl tutorial that requires reading 4 unsigned integers from a socket. I could not get more than 1 integer reading, and after digging, I found a solution. But I NEED to understand what I was not doing right (and went through a couple of Perl books, perldocs, etc. To no avail).
Example 1: Here is the successful solution code ( original ), suppose the socket connection is successful for both below:
{ local $/ = \16; # make <> read in 16 bytes with one swoop. my @integers = unpack "IIII", <$sock>; print "numbers: @val\n"; }
Example 2: I tried this below. If I print the input before unpacking, I get only one integer:
my $input; $sock->recv($input,16,0); my @integers = unpack("IIII", $input);
Concrete questions:
, .. Btw, " " - overthewire.org - .
1)
, <> , . , , - . . perldoc perlop ( <> ).
<>
perldoc perlop
$/ , "\n". ( slurping). . perldoc perlvar ( \number).
$/
perldoc perlvar
\number
TCP UDP?
recv - , <>/readline. recv(2). 4 4- , recv , , . 4 recv(), , , , TCP UDP.
recv
readline
recv(2)
recv()
TCP, , . 16- , , 16 , , . , , perl , 16- $/ = \16.
$/ = \16
, , <> -, read sysread ( OO, IO::Socket IO::Handle). , , , , .
read
sysread
IO::Socket
IO::Handle
readand readline(aka <>) wait for the required number of characters that will be available before returning. It will return fewer characters in case of an error or EOF, in which case the next read will return an error or EOF.
sysread returns as soon as some characters are available, even if they are less than the requested amount.
Based on what you say is recvsimilar to sysread. If you need 16 characters, you have to loop up to 16 characters or use reador readline.