Perl use socket to send binary data

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Recently the use of XML communication, using a custom protocol, the first to send XML the length of the file, and then the XML file.
Use Perl to send an integer or trouble, just like the python, so the python 3 is a bytes byte type.

use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => '127.0.0.1',
    PeerPort => '3565',
    Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";

my $xml = <<"END_XML";
<?xml version="1.0"?>
           <ip operation="show">
            </ip>
END_XML
my $size = length($xml);
# data to send to a server
my $req = 'hello world';
$size =pack("N",$size);
$size = $socket->send($size.$xml);

print "sent data of length $size\n";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $response\n";

$socket->close();

Pack Perl, $size =pack ("I", $size); use little endian
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Justin at March 01, 2014 - 12:04 PM