[Israel.pm] how to read a file to a Hash?

semuel semuel at semuel.co.il
Sat Jul 10 14:47:55 PDT 2004


Hello There.

Another thing is that I see that you are sending the content of some "url"
file to the client, but I don't see the client reading it. The client just
start by reading the "heshref". 
Please open the "url" file by writing "< url". It will make this open a
little more readable.
You need somehow to tell the client how many bytes he's going to receive for
the url file, so he can recv them, and separate it from the serialized hash.
You will also want to tell the client the size of the serialized hash, so he
will be able to cleanly receive it, with no limit on it's size.
(why won't you read the url file into a list of lines, serialize it, check
the size of the serialized file and hash, send both sized first, (in some
constant-sizs format) and then send the data itself)

And a correction to myself: please use nfreeze instead of freeze.

Semuel.

-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
Of liat
Sent: Saturday, July 10, 2004 10:22 PM
To: Perl in Israel
Subject: RE: [Israel.pm] how to read a file to a Hash?

hellow,
first of all thank you all for your help, i'm working on a project for my
studies, alone without a guide
and your help and directions are very useful and needed!!
actually i'm looking for a perl expert for a privat lessons (by pay of
course)  , so if anyone can and want to, plz let me know

about the project, those are the modules of the server and client sides

the server side===>>
    use strict;
    use Socket;
    use Carp;
    use Storable qw(nstore retrieve nstore_fd fd_retrieve);

    my $port = 2345||shift  ;
    ($port) = $port =~ /^(\d+)$/  or die "invalid port";

    #creating the socket
    my $proto = getprotobyname('tcp');
    socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";

    #the 'bind' func'
    my $sin= sockaddr_in($port, INADDR_ANY) ;  #the 'sin' will insert
    bind(Server,$sin )|| die "bind: $!";       # into the bind func'


    #the 'listen' func'
    listen(Server,SOMAXCONN)  || die "listen: $!";

    print "Server accepting clients!!!\n";
    my $paddr;

    #the 'accept' func'
	$paddr=accept(Client,Server) || die $!;


   if (open(FILE,"url"))
   {
   my $line = <FILE> ;
      while ($line)
      {
      send(Client,$line,0,)or die "send() failed: $!";
         $line= <FILE> ;
         }
   }

     my %hash = (
		"netvision" => {
			"adsl" => {
				"uname" => 'name',
				"upass" => 'pass',
			} ,
			"cable" => {
				"uname" => 'name',
				"upass" => 'pass',
			} ,
		},
		"zahav" => {
			"adsl" => {
				"uname" => 'name',
				"upass" => 'pass',
			} ,
			"cable" => {
				"uname" => 'name',
				"upass" => 'pass',
			} ,
			"dialup" => {
				"uname" => 'name',
				"upass" => 'pass',
			} ,
		},
	);

   nstore \%hash, 'file';
   my $hashref = retrieve('file');
   send(Client,$hashref,0,)or die "send() failed: $!";


the client side===>

use strict;
    use Socket;
    my ($remote,$port, $iaddr, $paddr, $proto);
	my $line;
    my @all_lines;
    my %hashref;

    $remote  = shift || '217.132.29.105';
    $port    = 2345||shift ;  # random port
    if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
    die "No port" unless $port;

    $proto   = getprotobyname('tcp');
    socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";

    $iaddr   = inet_aton($remote) || die "no host: $remote";
    $paddr   = sockaddr_in($port, $iaddr); #the paddr will insert
                                           # into the connect func'
	connect(SOCK, $paddr)    || die "connect: $!";



    my $hashref;

    recv(SOCK, $hashref,300,0)   or die "recv() failed: $!";

   my $isp;
   my %family;
   my $roles;
   my $person;
   my $kind;
   my $user;
   my $pass;
  while ( ($isp, $roles) = each %$hashref ) { ##need to write %$hashref in
order to get the
                                              ##hash of the reference the
'retrieve' returns
    print "$isp: ";
    while ( ($kind, $person) = each %$roles ) {
        print "$kind=> ";
         while ( ($user, $pass) = each %$person ) {
        print "$user=$pass ";
    }
    print "\n";
}
}


    close (SOCK)            || die "close: $!";
    exit;




it's not working of course becuase the sending of the "hashref" if not fits
to the client side
and as you see the client need to get the hash and printing it (kind of
testing)

regards
liat koski



-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il]On Behalf Of
Gabor Szabo
Sent: Saturday, July 10, 2004 7:20 PM
To: Perl in Israel
Subject: RE: [Israel.pm] how to read a file to a Hash?


Liat,
plese show relevant code examples so we can point to where to fix things.

It is a lot easire to help and it is also good because the examples remain
in the archive for the help of future generations....

Gabor

_______________________________________________
Perl mailing list
Perl at perl.org.il
http://perl.org.il/mailman/listinfo/perl


_______________________________________________
Perl mailing list
Perl at perl.org.il
http://perl.org.il/mailman/listinfo/perl






More information about the Perl mailing list