hari9 has asked for the wisdom of the Perl Monks concerning the following question:

Hi There.

Can somebody please suggest how I could dynamically update a variable's value in client's script,each time I call the server(which sends a value to client that is assigned to this client's variable).

for eg. If client has a variable $a=1, that it sends to server. server sends back client $t=2, that is assigned to $a of client.
Now when the same client establishes a connection with the server next time, it would send $a=2 ( and not $a=1).

for the following Server and Client codes:

Server
#tcpserver.pl use Switch; use feature qw(switch say); use IO::Handle; use IO::Socket::INET; $|=1; my ($socket,$client_socket); $socket = new IO::Socket::INET ( #LocalHost => '127.0.0.1', LocalPort => '9000', Proto => 'tcp', Listen => 5, Reuse => 1) or die "ERROR in Socket +Creation : $!\n"; while(1) { $client_socket = $socket->accept(); while(defined(my $data=<$client_socket>)) { $a=$data; } if($a eq 1) { $t=$a++; } $client_socket->shutdown(0); print $client_socket $t; } $socket->close();
Client
#tcpclient.pl use feature qw(switch say); use IO::Socket::INET; use IO::Handle; my($status,$md_self,@md_others); $|=1; $a=shift||1; my ($socket,$buffer); print "TCP Connection Success.\n"; $socket = new IO::Socket::INET (PeerHost => '127.0.0.1', PeerPort => '9000', Proto => 'tcp', ) or die "ERROR in Socket Creation + : $!\n"; # write on the socket to server. print $socket $a; $socket->shutdown(1); $t=<$socket>; print $t; $a=$t; $socket->close();
Thanks.

Replies are listed 'Best First'.
Re: dynamic socket communication
by zentara (Cardinal) on Aug 05, 2010 at 17:26 UTC
    There is a way to write variables to the DATA section of your script, when the script exits, and you could store the current value there, and reread it everytime you start up, but it is cleaner to use the Inline::Files module. The hard way:
    #!/usr/bin/perl use warnings; use strict; # The DATA filehandle is just a an open handle on the current # file. The current file is just the $0 variable (so long as # you haven't changed it) # main part of script ... my $count; while(<DATA>){ $count = $_ ; print "$count\n";} $count++; # then redo the count update: rewrite_count($count); sub rewrite_count { my $count = shift; open(SELF,"+<$0")||die $!; while(<SELF>){last if /^__END__/} truncate(SELF,tell SELF); print SELF $count; close SELF; } __END__ 1232

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

      I would cringe if I saw this approach in production. I would be too concerned with breakage or concurrency issues trashing the file. Personally, I would feel better with an external file.

      Also allows locking down the source. The way this stands, the user running the application also has access to modify the source code.

      --MidLifeXis

Re: dynamic socket communication
by jethro (Monsignor) on Aug 05, 2010 at 17:27 UTC

    Store it in a file.

    If your data is more complicated you could even use a data serialization module like Data::Dumper, Storable or YAML.

    You might have to use file locking if more than one client script might be started at the same location at the same time (and both need to access and change the same value)

Re: dynamic socket communication
by wol (Hermit) on Aug 06, 2010 at 15:15 UTC
    It's not obvious from the original post whether the value returned by the server would need to be maintained between one invocation of the client and the next.

    The whole thing seems reminiscent of a web browser maintaining cookies, and cookies come in two flavours: persistent cookies: saved to disk when the browser is shut down; and session cookies: never written to disk and lost when the browser is quit.

    Which flavour are we talking about here?

    And furthermore, would you like a nice glass of milk with that?

    use JAPH;
    print JAPH::asString();