perl使用socket 发送二进制数据
来源:互联网 发布:中国卫星通信软件下载 编辑:程序博客网 时间:2024/06/06 23:43
最近使用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";
<?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();
使用perl的pack处理,$size =pack("i",$size);使用小端字节序
使用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";
<?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();
使用perl的pack处理,$size =pack("i",$size);使用小端字节序
0 0
- perl使用socket 发送二进制数据
- perl socket 返回发送成功数据的长度
- perl put 发送数据
- perl socket 客户端发送消息
- Socket使用Linux SSH代理发送数据
- 使用Socket类接收和发送数据
- C#使用socket发送和接受数据
- 使用Socket类接收和发送数据
- perl post发送json数据
- perl get 发送json数据
- Python使用struct处理二进制 解析二进制数据 解析socket数据
- Python使用struct处理二进制 解析二进制数据 解析socket数据
- Perl -- 使用Perl 读取和发送邮件
- socket数据发送过程
- socket发送数据
- php socket接收二进制数据
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)
- Win8笔记本不能正常关机或重启
- WebKit源代码目录结构
- Populating Next Right Pointers in Each Node和 II
- HBase Bulkload bug修复及patch提交
- javascript 得到图片的宽高
- perl使用socket 发送二进制数据
- Contour Detection and Image Segmentation 简介
- 用busybox制作rootfs && yaffs文件系统
- 什么情况下发生单块读?
- VMWare中网络
- 获取JS链接带的参数
- C++11特性乱弹
- php pdo用法例解
- linux SecureCRT ssh key认证登陆