【发布时间】: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