in reply to Re^2: Problem Transmitting Data via TCP/IP
in thread Problem Transmitting Data via TCP/IP
But what do you mean "wrongly coded"? How should htonl() be defined?
Your htonl:
sub htonl { my $input = shift; my $output = unpack( 'N*', pack( 'L*', $input ) ); return $output; }
Which means that the return from that first pack is 4 bytes and encoded in whatever byte-order (endianess) your current platform uses:
$packed = pack 'l', 17;; print length $packed;; 4 print ord( substr $packed, $_, 1 ) for 0 .. 3;; 17 0 0 0
On my intel system, that means little-endian (the low byte comes first).
$unpacked = unpack 'N', $packed;; print length( $unpacked );; 9 print ord( substr $unpacked, $_, 1 ) for 0 .. 9;; 50 56 53 50 49 50 54 55 50 0
Resulting in a 9-byte ascii encode string containing the number: 285212672; which is meaningless.
htonl() could be correctly coded as sub htonl{ pack 'l>', $_[0] }; if you see the need for wrapping a built-in function in a silly named wrapper :)
Similarly, you will need to fix your ntohl(); something like this: sub ntohl{ unpack 'l>', $_[0] } would suffice.
That may fix the second part of your problem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Problem Transmitting Data via TCP/IP
by scorpio17 (Canon) on Aug 09, 2012 at 20:12 UTC |