Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4142377
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: PERL

2014-01-23 14:06:53

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

dizhuang2014-01-28 15:01:56

真是麻烦啊,用socket处理的话。