我如何通过套接字共享Perl数据结构? [英] How can I share Perl data structures through a socket?

查看:110
本文介绍了我如何通过套接字共享Perl数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



服务器程序:

 使用IO :: Socket; 
使用strict;
使用警告;

my%hash =(name=>pavunkumar,age=> 20);
my $ new = \%hash;
#打开系统变量缓冲输出
$ | = 1;
#创建一个新的套接字
my $ socket =
IO :: Socket :: INET-> new(LocalPort => 5000,Proto =>'tcp',Localhost => ;
'localhost','Listen'=> 5,'Reuse'=> 1);
die无法创建$!\\\
除非($ socket);
打印\ nUDPServer等待端口5000 \\\
;
my $ new_sock = $ socket-> accept();
my $ host = $ new_sock-> peerhost(); $(< $ new_sock>)
{
#my $ line =< $ new_sock>
;
打印Dumper$ host $ _;
打印$ new_sock $ new。 \\\
;
}
print$ host is closed \\\
;

客户端程式

 使用IO :: Socket; 
使用Data :: Dumper;
使用警告;
使用strict;

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

my $ ref = \%hash;


#这个客户端连接指定的以下地址和端口
#INET函数将创建套接字文件并建立与
#服务器
的连接
my $ port = shift || 5000;
my $ host = shift || 本地主机;
my $ recv_data;
my $ send_data;
my $ socket = new IO :: Socket :: INET(
PeerAddr => $ host,
PeerPort => $ port,
Proto =>'tcp' ,)

或死亡无法连接到Server \ n;
while(1)
{
my $ line =< stdin> ;
print $ socket $ ref。\\\
;
if($ line =< $ socket>)
{
print Dumper $ line;
}
else
{
printServer is closed \\\
;
最后;
}

}

我给出了我的示例程序我在做什么。任何人都可以告诉我在这段代码中我正在做
错误吗?我需要做什么来访问哈希值?

解决方案

当您说

  print $ ref; 

,部分指示Perl将 $ ref 放入一个字符串中(因为只有字符串可以是 print ed)。事实证明,默认情况下引用不会变成非常有用的字符串。



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

程序上,您可以说[1]

 使用Storable qw(nfreeze); #nfreeze而不是冻结,因为
#我们想要一个网络可移植字符串
...
print nfreeze($ ref);

一方和

 使用Storable qw(解冻); 
...
my $ ref = thaw($ line);

另一方面。

还有一个OO接口;请阅读 Storable 文档获取更多信息。



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


In sockets I have written the client server program. First I tried to send the normal string among them it sends fine. After that I tried to send the hash and array values from client to server and server to client. When I print the values using Dumper, it gives me only the reference value. What should I do to get the actual values in client server?

Server Program:

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

Client Program

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

}

I have given my sample program about what I am doing. Can any one tell me what I am doing wrong in this code? And what I need to do for accessing the hash values?

解决方案

When you say

print $ref;

, you're in part instructing Perl to turn $ref into a string (since only strings can be printed). It turns out that references don't turn into very useful strings by default.

You need to turn $ref into a string that you can send across the wire and then decode on the other side to get the data back out. This process is referred to as "serialization". Data::Dumper's output is actually a valid serialization of its arguments, but the basic serialization module in Perl is Storable.

Procedurally, you can say[1]

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

on one side and

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

on the other.

There's also an OO interface; read the Storable documentation for more information.

[1]: Notice the yaddayaddas. This is incomplete code that merely illustrates the key differences from your code.

这篇关于我如何通过套接字共享Perl数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆