加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl socket传hash

发布时间:2020-12-16 00:38:12 所属栏目:大数据 来源:网络整理
导读:http://www.perlmonks.org/?node_id=718648 # server.pl use strict; use warnings; use Data::Dumper; use JSON::XS; use IO::Socket; use Sys::Hostname; use constant BUFSIZE = 1024; my $JSONObject = JSON::XS-new-ascii-pretty-allow_nonref(); my $h

http://www.perlmonks.org/?node_id=718648

# server.pl

use strict;
use warnings;

use Data::Dumper;
use JSON::XS;
use IO::Socket;
use Sys::Hostname;
use constant BUFSIZE => 1024;

my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();

my $host?? = hostname;
my $port?? = shift || '10280';
my $socket = new IO::Socket(
? Domain??? => PF_INET,
? Proto???? => getprotobyname('tcp'),
? LocalAddr => $host,
? LocalPort => $port,
? Listen??? => 1,?????????????????????? #SOMAXCONN,
??????????????????????????????????????? #ReuseAddr => SO_REUSEADDR,
) or die $@;
my $buffer;

print "Waiting to do service...n";
while (my $client = $socket->accept) {
? print "Client: ",$client->peerhost," Connected..n";
? syswrite($client,"Reached Servern",BUFSIZE);
? if (sysread($client,$buffer,BUFSIZE) > 0) {
??? my @AoH = $JSONObject->decode($buffer);
??? print "AoH: " . Dumper(@AoH);
? }
}

?

-----------------# client.pl

use strict;
use warnings;

use JSON::XS;
use IO::Socket;
use constant BUFSIZE => 1024;
my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();

my @AoH = (
? {
??? husband => "barney",
??? wife??? => "betty",
??? son???? => "bamm bamm",
? },
? {
??? husband => "george",
??? wife??? => "jane",
??? son???? => "elroy",
? {
??? husband => "homer",
??? wife??? => "marge",
??? son???? => "bart",
);

my $host = shift or die "Usage: client.pl host [port]n";
my $port = shift || '10280';

my $socket = new IO::Socket(
? Domain?? => PF_INET,
? PeerAddr => $host,
? PeerPort => $port,
? Proto??? => getprotobyname('tcp'),
? Timeout? => 60,
) or die $@;

my $buffer;
if (sysread($socket,BUFSIZE) > 0) {
? syswrite(STDOUT,$buffer);
}

syswrite($socket,$JSONObject->encode(@AoH),BUFSIZE);
close($socket);

-------------------------

接受中文字符解码:

? my $ll = encode("gb2312",decode("utf8",$mm->{$_}));

?

最简单的json使用

client: use JSON; $heap->{json} = JSON->new->ascii; my $j_param = $heap->{json}->encode(%color); $heap->{server}->put($j_param); server: my $c_par = from_json($input);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读