Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: No data received on client socket

by ikegami (Patriarch)
on Sep 19, 2006 at 17:11 UTC ( [id://573771]=note: print w/replies, xml ) Need Help??


in reply to No data received on client socket

recv is for datagram protocols (like UDP). You need to use sysread when using a stream protocol (like TCP).

# Send request. print $oSocket $sSend or die("Unable to write to the socket: $!\n"); # Receive response. my $data_read = ""; my $rv; do { $rv = sysread($oSocket, $data_read, 4096, length($data_read)); die("Unable to read from the socket: $!\n") if not defined $rv; } while $rv;

Tested.

Update: Added code.

Replies are listed 'Best First'.
Re^2: No data received on client socket
by rbi (Monk) on Sep 19, 2006 at 17:24 UTC
    I've tried it, but it does not work.
    By the way, on old installations it works with recv().
    I've read that recv() shrinks to a smaller size if the last chunk of bytes is shorter than the specivfied size (4096 in this case), while sysread tries to read the specified size.

    Update I've tested your implementation and - as with the original code - I can see with tcpdump the query results coming back as network traffic but I get nothing in Perl.
      Does the server close the socket after the response is sent? My code assumed so. The following looks for </xml> instead.
      # Receive response. my $read_buf = ""; my $response; for (;;) { my $rv = sysread($oSocket, $read_buf, 4096, length($read_buf)); die("Unable to read from the socket: $!\n") if not defined $rv; die("Premature end of data\n") if not $rv; if ($read_buf =~ s{(.*</xml>)}{}si) { $response = $1; last; } }

      Tested. Both version work fine for me (although the first requires that the server closes the socket after sending the response).

      I've read that recv shrinks to a smaller size if the last chunk of bytes is shorter than the specivfied size (4096 in this case), while sysread tries to read the specified size.

      sysread is never guaranteed to return the number of bytes requested. When reading from a socket, it returns when LENGTH bytes are available and after each (non-empty?) packet received. And left-over bytes are returned by the next call to sysread.

      By the way, Type => 'SOCK_STREAM' should be Type => SOCK_STREAM, and specifying both Type and Proto is redundant.

        Thank you very much for all this, but I still don't get anything ("Premature end of data" from your code).
        I'm ignorant - among other things :) - about sockets. I thought it could be something not depending from the Perl script istself but from something at "lower level" since the script works just the same on other older Linux computers. Also, on this new Linux computer the access to the same database with the same test query works with a php implementation, as I've just found out.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found