Distributed programming: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Perl}}: future-proof for 5.36)
Line 1,150: Line 1,150:
=={{header|Perl}}==
=={{header|Perl}}==
Using Data::Dumper and Safe to transmit arbitrary data structures as serialized text between hosts. Same code works as both sender and receiver.
Using Data::Dumper and Safe to transmit arbitrary data structures as serialized text between hosts. Same code works as both sender and receiver.
<syntaxhighlight lang="perl">use Data::Dumper;
<syntaxhighlight lang="perl">use strict;
use warnings;
use Data::Dumper;
use IO::Socket::INET;
use IO::Socket::INET;
use Safe;
use Safe;


sub get_data {
sub get_data {
my $sock = new IO::Socket::INET
my $sock = IO::Socket::INET->new(
LocalHost => "localhost",
LocalHost => "localhost",
LocalPort => "10000",
LocalPort => "10000",
Proto => "tcp",
Proto => "tcp",
Listen => 1,
Listen => 1,
Reuse => 1;
Reuse => 1);
unless ($sock) { die "Socket creation failure" }
unless ($sock) { die "Socket creation failure" }
my $cli = $sock->accept();
my $cli = $sock->accept();
Line 1,166: Line 1,168:
# of course someone may be tempted to send you 'system("rm -rf /")',
# of course someone may be tempted to send you 'system("rm -rf /")',
# to be safe(r), use Safe::
# to be safe(r), use Safe::
my $safe = new Safe;
my $safe = Safe->new;
my $x = $safe->reval(join("", <$cli>));
my $x = $safe->reval(join("", <$cli>));
close $cli;
close $cli;
Line 1,176: Line 1,178:
my $host = shift;
my $host = shift;
my $data = shift;
my $data = shift;
my $sock = new IO::Socket::INET
my $sock = IO::Socket::INET->new(
PeerAddr => "$host:10000",
PeerAddr => "$host:10000",
Proto => "tcp",
Proto => "tcp",
Reuse => 1;
Reuse => 1);


unless ($sock) { die "Socket creation failure" }
unless ($sock) { die "Socket creation failure" }