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