最近使用xml进行通讯,使用自定义的协议,最先发送xml文件的长度,然后是xml文件。
使用perl发送一个整数还是比较麻烦的,这和python一样,所以python 3才有了bytes字节类型。
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";
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();
使用perl的pack处理,$size =pack("i",$size);使用小端字节序
阅读(7717) | 评论(1) | 转发(1) |