【问题标题】:How can I share Perl data structures through a socket?如何通过套接字共享 Perl 数据结构?
【发布时间】:2011-02-18 22:37:39
【问题描述】:

在套接字中,我编写了客户端服务器程序。首先,我尝试在它们之间发送正常的字符串,它可以正常发送。之后,我尝试将哈希值和数组值从客户端发送到服务器和服务器到客户端。当我使用 Dumper 打印值时,它只给我参考值。我应该怎么做才能获取客户端服务器中的实际值?

服务器程序:

use IO::Socket;
use strict;
use warnings;

my %hash = ( "name" => "pavunkumar " , "age" => 20 ) ;
my $new  = \%hash ;
#Turn on System variable for Buffering output
$| = 1;
# Creating a a new socket
my $socket=
IO::Socket::INET->new(LocalPort=>5000,Proto=>'tcp',Localhost =>
        'localhost','Listen' => 5 , 'Reuse' => 1 );
die "could not create $! \n" unless ( $socket );
        print "\nUDPServer Waiting port 5000\n";
        my $new_sock =  $socket->accept();
        my $host =  $new_sock->peerhost();
        while(<$new_sock>)
        {
        #my $line = <$new_sock>;
        print Dumper "$host $_";
        print $new_sock $new . "\n";
        }
        print "$host is closed \n" ;

客户计划

use IO::Socket;                               
use Data::Dumper ;                            
use warnings ;                                
use strict ;                                  

my %hash  = ( "file" =>"log.txt" , size => "1000kb") ;

my $ref = \%hash ;


# This client for connecting the specified below address and port 
# INET function will create the socket file and establish the connection with
# server

my $port = shift || 5000 ;
my $host = shift || 'localhost';
my $recv_data ;
my $send_data;
my $socket = new IO::Socket::INET (
                                  PeerAddr  => $host ,
                                  PeerPort  => $port ,
                                  Proto => 'tcp', )

or die "Couldn't connect to Server\n";
while (1)
{
        my $line = <stdin> ;
        print $socket $ref."\n";
        if ( $line = <$socket> )
        {
                print Dumper  $line ;
        }
        else
        {
                print "Server is closed \n";
                last ;
        }

}

我已经给出了关于我在做什么的示例程序。谁能告诉我我在做什么 这段代码错了吗?我需要做什么来访问哈希值?

【问题讨论】:

    标签: perl sockets serialization hash


    【解决方案1】:

    当你说

    print $ref;
    

    ,你在某种程度上指示 Perl 将 $ref 转换为字符串(因为只有字符串可以是 printed)。事实证明,默认情况下引用不会变成非常有用的字符串。

    您需要将$ref 转换为可以通过网络发送的字符串,然后在另一端解码以获取数据。这个过程被称为“序列化”。 Data::Dumper 的输出实际上是其参数的有效序列化,但 Perl 中的基本序列化模块是 Storable

    程序上,你可以说[1]

    use Storable qw(nfreeze); # nfreeze rather than freeze because
                              # we want a network-portable string
    ...
    print nfreeze($ref);
    

    在一侧和

    use Storable qw(thaw);
    ...
    my $ref = thaw($line);
    

    另一方面。

    还有一个面向对象的接口;阅读Storable 文档了解更多信息。

    [1]:注意 yadddayaddas。这是不完整的代码,仅说明了与您的代码的主要区别。

    【讨论】:

    • 好答案。值得强调的是,您使用了 nfreeze(而不是 freeze),这有助于使 Storable 编码数据更便携。
    【解决方案2】:

    问题是您发送的是对数据的引用,而不是数据本身。您需要以某种方式序列化数据。 JSON 是一种非常简单的方法。还有YAML

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2010-11-22
      相关资源
      最近更新 更多