⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rpcdemo.pl

📁 perl learn perl by examples
💻 PL
字号:
use RPC; 

my $host = 'localhost';
my $port = 1300;
my $prog;

foreach $arg (@ARGV) {
    if ($arg eq '-server') {
        server_start();
    } elsif ($arg eq '-client') {
        client_start();
    }
}

#----------------------------------------------------------------------
# Server stuff
sub ask_sheep { 
    print STDERR "Question: @_\n";
    return "No";
}

sub add {
    my ($a, $b) = @_;
    return wantarray ? ('The result is ', $a+$b) : $a+$b;
}

sub server_start() {
    RPC->new_server($host, $port);
    print "RPC Server created\nStart the client now\n";
    RPC->event_loop();
}

#----------------------------------------------------------------------
# Client stuff
sub client_start() {
    $conn1 = RPC->connect($host, $port);
    print "Client RPC connection initialized. Sending msgs\n";
    my $i;
    my $answer;
    my $question = "Ba ba black sheep, have you any wool ?";
    print "Question: $question\n";
    $answer = $conn1->rpc('ask_sheep', $question);
    print "Answer:   $answer\n\n";

    print "Question: Sum of 10 and 34.5?\n";
    ($a, $b) = $conn1->rpc ('add', 10, 34.5);
    # Note: "add" in the server checks wantarray
    print "Answer:   $a $b\n";
}

exit(0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -