如何通过套接字共享Perl数据结构?

如何通过套接字共享Perl数据结构?,perl,sockets,serialization,hash,Perl,Sockets,Serialization,Hash,在sockets中,我编写了客户机-服务器程序。首先,我尝试在它们之间发送普通字符串,它发送的很好。之后,我尝试将哈希值和数组值从客户端发送到服务器,从服务器发送到客户端。当我使用转储程序打印值时,它只给我参考值。我应该怎么做才能获得客户机服务器中的实际值 服务器程序: use IO::Socket; use strict; use warnings; my %hash = ( "name" => "pavunkumar " , "age" => 20 ) ; my $new =

在sockets中,我编写了客户机-服务器程序。首先,我尝试在它们之间发送普通字符串,它发送的很好。之后,我尝试将哈希值和数组值从客户端发送到服务器,从服务器发送到客户端。当我使用转储程序打印值时,它只给我参考值。我应该怎么做才能获得客户机服务器中的实际值

服务器程序:

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 ;
        }

}
使用IO::Socket;
严格使用;
使用警告;
我的%hash=(“姓名”=>“pavunkumar”,“年龄”=>20);
my$new=\%散列;
#打开系统变量以缓冲输出
$| = 1;
#创建一个新的套接字
我的$socket=
IO::Socket::INET->new(LocalPort=>5000,Proto=>tcp',Localhost=>
‘localhost’、‘Listen’=>5,‘重用’=>1);
模具“无法创建$!\n”,除非($socket);
打印“\nUDPServer等待端口5000\n”;
我的$new_sock=$socket->accept();
my$host=$new_sock->peerhost();
while()
{
#我的$line=;
打印转储程序“$host$\”;
打印$new\u sock$new。“\n”;
}
打印“$host已关闭\n”;
客户端程序

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 ;
        }

}
使用IO::Socket;
使用数据::转储程序;
使用警告;
严格使用;
我的%hash=(“文件”=>“log.txt”,大小=>“1000kb”);
my$ref=\%散列;
#此客户端用于连接下面指定的地址和端口
#INET函数将创建套接字文件并与建立连接
#服务器
我的$port=shift | | 5000;
my$host=shift | |“localhost”;
我的$recv_数据;
我的$send_数据;
my$socket=新IO::socket::INET(
PeerAddr=>$host,
PeerPort=>$port,
Proto=>tcp',)
或“无法连接到服务器\n”;
而(1)
{
我的$line=;
打印$socket$ref.“\n”;
如果($line=)
{
打印转储程序$行;
}
其他的
{
打印“服务器已关闭\n”;
最后;
}
}
我已经给出了我正在做的示例程序。有人能告诉我我在做什么吗
这个代码有错吗?我需要做什么来访问散列值

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

当你说

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

您需要将
$ref
转换成一个字符串,然后通过导线发送,然后在另一端解码以将数据返回。此过程称为“序列化”<代码>数据::转储程序的输出实际上是其参数的有效序列化,但Perl中的基本序列化模块是

按照程序,你可以说[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);
另一方面

还有一个OO接口;有关更多信息,请阅读可存储的
文档


[1] :注意雅达雅达。这是一段不完整的代码,仅说明了与代码的关键区别。

回答得好。值得强调的是,您使用了
nfreeze
(而不是
freeze
),这有助于使可存储的编码数据更加便携。