RPC::pClient(3pm) | User Contributed Perl Documentation | RPC::pClient(3pm) |
RPC::pClient - Perl extension for writing pRPC clients
use RPC::pClient; $sock = IO::Socket::INET->new('PeerAddr' => 'joes.host.de', 'PeerPort' => 2570, 'Proto' => 'tcp'); $connection = new RPC::pClient('sock' => $sock, 'application' => 'My App', 'version' => '1.0', 'user' => 'joe', 'password' => 'hello!');
pRPC (Perl RPC) is a package that simplifies the writing of Perl based client/server applications. RPC::pServer is the package used on the server side, and you guess what RPC::pClient is for. See RPC::pClient(3) for this part.
pRPC works by defining a set of of functions that may be executed by the client. For example, the server might offer a function "multiply" to the client. Now a function call
@result = $con->Call('multiply', $a, $b);
on the client will be mapped to a corresponding call
multiply($con, $data, $a, $b);
on the server. (See the funcTable description below for $data.) The function calls result will be returned to the client and stored in the array @result. Simple, eh? :-)
$client = RPC::pClient->new ( ... ); if (!ref($client)) { print STDERR "Error while creating client object: $client\n"; } else { # Do real stuff ... }
$c = Add($a, $b) # Use $c ...
is
$c = $client->Call("Add", $a, $b); if ($client->error) { # Do something in case of error ... } else { # Use $c ... }
my($status, @result) = $client->CallInt("Add", $a, $b); if (!$status) { # Do something in case of error my $errmsg = shift @result || "Unknown error"; ... } else { ... }
# Get the current encryption mode $mode = $server->Encrypt(); # Currently disable encryption $server->Encrypt(undef); # Switch back to the old mode $server->Encrypt($mode);
Client attributes will typically be supplied with the "new" constructor.
Note that you can set or remove encryption on the fly (putting "undef" as attribute value will stop encryption), but you have to be sure, that both sides change the encryption mode.
Do not modify this attribute directly, use the encrypt method instead! However, it is legal to pass the attribute to the constructor.
Example:
use Crypt::DES; $crypt = DES->new(pack("H*", "0123456789abcdef")); $client->Encrypt($crypt); # or, to stop encryption $client->Encrypt(undef);
#!/usr/local/bin/perl -T use 5.0004; # Yes, this really *is* required. use strict; # Always a good choice. use IO::Socket(); use RPC::pClient; # Constants my $MY_APPLICATION = "Test Application"; my $MY_VERSION = 1.0; my $MY_USER = "foo"; my $MY_PASSWORD = "bar"; # Connect to the server my $sock = IO::Socket::INET->new('PeerAddr' => 'joes.host.de', 'PeerPort' => 5000, 'Proto' => 'tcp'); if (!defined($sock)) { die "Cannot connect: $!\n"; } # Login procedure my $client = RPC::pClient->new('sock' => $sock, 'application' => $MY_APPLICATION, 'version' => $MY_VERSION, 'user' => $MY_USER, 'password' => $MY_PASSWORD); if (!ref($client)) { die "Cannot create client: $client\n"; } # Call multiply function my $a = $client->Call("multiply", 3, 4); if ($client->error) { die "An error occurred while multiplying: $a\n"; }
Jochen Wiedmann, wiedmann@neckar-alb.de
pRPC::Server(3), Storable(3), Sys::Syslog(3)
For an example application, see DBD::pNET(3).
2022-12-04 | perl v5.36.0 |