Leser: 1
|< 1 2 3 4 >| | 31 Einträge, 4 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use strict;
use warnings;
package Server;
use Storable qw(fd_retrieve);
use Data::Dumper;
use IO::Socket::INET;
sub run {
my $server = IO::Socket::INET->new( LocalPort => 43610
, Type => SOCK_STREAM
, Listen => 10
, Reuse => 1
) or die $!;
while (my $client = $server->accept()) {
my $data = fd_retrieve($client);
close $client;
close $server;
print Dumper($data);
}
}
package Client;
use Storable qw(store_fd);
use IO::Socket::INET;
sub run {
sleep 1;
my $socket = IO::Socket::INET->new( PeerAddr => '127.0.0.1'
, PeerPort => 43610
, Proto => 'tcp'
, Type => SOCK_STREAM
) or die $!;
my %hash = (a=>1,b=>2,c=>3);
store_fd \%hash, $socket;
}
1;
if (my $pid = fork) {
run Server; waitpid($pid,0);
} else {
run Client;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use strict;
use warnings;
package Server;
use IO::Socket::SSL;
use Storable qw(nstore_fd);
use Data::Dumper;
unless (-d "certs") {
if (-d "../certs") {
chdir "..";
} else {
die "Please run this example from the IO::Socket::SSL distribution directory!\n";
}
}
sub run {
my $socket = IO::Socket::SSL->new(
Listen => 5,
LocalAddr => 'localhost',
LocalPort => 9000,
Proto => 'tcp',
Reuse => 1,
SSL_verify_mode => 0x01,
SSL_passwd_cb => sub {return "bluebell"},
) or die "server: can't open socket over port 9000";
warn "server initialized\n";
while (my $client = $socket->accept()) {
chomp (my $request = <$client>);
next unless $request;
warn "client request: $request\n";
my %hash = (a=>1,b=>2,c=>3);
nstore_fd(\%hash, $client) or die $!;
close($client);
close($socket);
}
}
package Client;
use IO::Socket::SSL;
use Storable qw(fd_retrieve);
use Data::Dumper;
sub run {
my $socket = IO::Socket::SSL->new(
PeerAddr => 'localhost',
PeerPort => '9000',
Proto => 'tcp',
SSL_use_cert => 1,
SSL_verify_mode => 0x01,
SSL_passwd_cb => sub { return "opossum" }
) or die "client: can't connect to 127.0.0.1:9000";
warn "client connected to server\n";
print $socket "hash\n";
my $data = fd_retrieve($socket) or die $!;
warn Dumper($data);
close($socket);
}
1;
if (my $pid = fork) {
run Server;
waitpid($pid,0);
} else {
sleep 1;
run Client;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use strict;
use warnings;
use Benchmark;
use Data::Dumper;
use YAML;
use JSON;
use XML::Dumper;
use Storable qw(nfreeze thaw);
use Safe;
my $struct = {
s1 => [qw(v1 v2 v3 v4 v5 v6 v7 v8 v9 v0)],
s2 => {
s3 => [qw(v11 v12 v13 v14 v15 v16 v17 v18 v19 v20)],
s4 => 'v21',
},
s5 => [ { s6 => 'v22', s7 => 'v23' }, { s8 => 'v24', s9 => 'v25' } ],
s0 => 'v26',
};
sub xml_dumper {
my $dmp = new XML::Dumper;
my $p2s = $dmp->pl2xml($struct);
my $s2p = $dmp->xml2pl($p2s);
}
sub json {
my $json = new JSON;
my $p2s = $json->objToJson($struct);
my $s2p = $json->jsonToObj($p2s);
}
sub storable {
my $safe = new Safe;
$safe->permit(qw(:default require));
{
no warnings 'once';
$Storable::Deparse = 1;
$Storable::Eval = sub { $safe->reval($_[0]) };
}
my $p2s = nfreeze $struct;
my $s2p = thaw $p2s;
}
sub data_dumper {
my $p2s = Dumper($struct);
my $safe = new Safe;
my $s2p = $safe->reval($p2s);
}
sub yaml {
my $p2s = Dump($struct);
my $s2p = Load($p2s);
}
Benchmark::cmpthese(-1,{
'xml-dumper' => \&xml_dumper,
'storable' => \&storable,
'data-dumper' => \&data_dumper,
'yaml' => \&yaml,
'json' => \&json,
});
|< 1 2 3 4 >| | 31 Einträge, 4 Seiten |