在套接字中我编写了客户端服务器程序。首先我试图发送正常的字符串,它发送正常。之后,我尝试将哈希和数组值从客户端发送到服务器和服务器到客户端。当我使用Dumper打印这些值时,它只给出参考值。我应该怎么做才能获得客户端服务器的实际值?如何通过套接字共享Perl数据结构?
服务器程序:
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 ;
}
}
我已经给约我在做什么我的示例程序。任何人都可以告诉我我在做什么 这段代码错了吗?我需要做什么来访问哈希值?
很好的答案。值得强调的是,您使用了'nfreeze'(而不是'freeze'),这有助于使可存储的编码数据更加便携。 – daotoad 2010-05-13 05:37:21
谢谢,完成。 – darch 2010-05-13 06:24:12