Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Problem Transmitting Data via TCP/IP

by scorpio17 (Abbot)
on Aug 09, 2012 at 19:12 UTC ( #986590=note: print w/replies, xml ) Need Help??

in reply to Re: Problem Transmitting Data via TCP/IP
in thread Problem Transmitting Data via TCP/IP

Thank you! I knew it had to be something simple like that.

But what do you mean "wrongly coded"? How should htonl() be defined?

When I change my code to this:

# $socket->send( htonl($request_length) ) or die "3a Couldn't write + to socket: $!"; $socket->send( pack('l>',$request_length) ) or die "3a Couldn't wr +ite to socket: $!"; $socket->send( $request ) or die "3b Couldn't write to socket: $!" +;

I get the expected output in the logging console, and the server seems to write the response to the socket, but my client fails to read the response from the socket.

I get this message now:

5 Couldn't read data from server:

So something is still not quiet right... but I'm getting closer!

Replies are listed 'Best First'.
Re^3: Problem Transmitting Data via TCP/IP
by BrowserUk (Pope) on Aug 09, 2012 at 19:33 UTC
    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; }
    1. First, takes the number and "packs it" (converts it to binary) as a signed long:

      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).

    2. But then, you unpack (convert binary to ascii), those 4 bytes, treating them a big-endian long ('N'), resulting in:
      $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.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks again. I've never taken the time to learn how pack and unpack work as well as I should, and it looks like that mistake has finally caught up with me.

      For what it's worth - the original C++ code (which I didn't write) used the functions htonl() and ntohl(). I was not familiar with those, but could tell that it had something to do with encoding/decoding data, so I googled for a perl implementation, and found one in the Net::Inet module on CPAN. It's written to take an array of inputs, but I knew that in my case I'd only be sending in a single argument.

      As it turns out, my code did have another error: recv() returns undef if there's an error, so I had to change this:

      # this is broken !!! $socket->recv($data, $reponse_length) or die "Couldn't read data from +server: $!";

      into this:

      unless (defined $socket->recv($data, $reponse_length)) { die "Couldn't read data from server: $!"; }

      But everything seems to be working now -thank you!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986590]
[Corion]: Meh. I need to find myself a better "programming" feed than r/programming (which is just HackerNews reposts and advertisements, very little code). Maybe I should select a list of links that I liked on r/programming and then google for ...
[Corion]: ... an aggregator site that also listed (ideally) all of these links once. And maybe also have an exclude list to blacklist some of the most spammy links that the site may have never mentioned at all
[marto]: I gave up reading HN at all, and feel much better for it :P
[Corion]: Now, how to best automate that Google search ... :-)
[Corion]: marto: Yeah, I'm also short of not reading it at all. I already stopped reading it in the morning because it gave me a foul mood.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2017-07-24 13:22 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (354 votes). Check out past polls.