请教关于perl下socket编程,如何发送和接收十六进制数

请教关于perl下socket编程,如何发送和接收十六进制数

请教关于perl下socket编程,如何发送和接收十六进制数

主要是该如何接收十六进制数呢,目前有个服务端程序在接收相应指令后,返回的结果是十六进制数,不知道该如何接收。。
以下是我的客户端程序,接收到十六进制数的时候就会出现乱码,,请各位帮忙看看该如何改呢,

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl

use strict;
use Socket;
my ($name,$len,$aliases,$proto,$type,$ServerAddr,$buffer);
my $server = $ARGV[0] || '127.0.0.1';
my $port = $ARGV[1] || '11122';

my $PF_INET=2;
my $SOCK_STREAM=1;
$proto=getprotobyname("tcp");
$|=1;
my $BUFFER = "$ARGV[2]";
#my $BUFFER = "\$002";
$BUFFER .= chr(13);
($name,$aliases,$type,$len,$ServerAddr) = gethostbyname($server);
my $ADDR=pack('Sna4x8',$PF_INET,$port,$ServerAddr);

socket (SOCKET,$PF_INET,$SOCK_STREAM,$proto) or die "Can't create socket: $!";
connect (SOCKET,$ADDR) or die "Can't connect: $!";

syswrite(SOCKET, $BUFFER, length($BUFFER));
sysread(SOCKET, $buffer, 200);
print $buffer;
print "\n";
exit(0);

print hex $buffer;

不行?
pack unpack
用的贼爽


QUOTE:
原帖由 flw 于 2008-8-26 17:20 发表
pack unpack
用的贼爽

确实


[Copy to clipboard] [ - ]
CODE:
sub hex_string {
    my $string = shift;
    jion " ", map{sprintf "%02X", ord($_)} split //, $string;
}