Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32699
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-03 11:25
文章分类
文章存档

2014年(9)

我的朋友

分类: PERL

2014-01-27 14:52:41

最近使用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);使用小端字节序
阅读(1298) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~