Thread Hash Struktur senden: Client - Server (30 answers)
Opened by bloonix at 2006-08-20 07:23

bloonix
 2006-11-29 09:37
#69044 #69044
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hallo ptk,

der Autor von IO::Socket::SSL hat auf meinen -> Bug-Report <- geantwortet.

Vielleicht ist das auch für dich interessant...

Das Problem habe ich auch genauso gelöst, wie mir der Autor mit pack(),
unpack() und read() vorgeschlagen hat.

Code: (dl )
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use strict;
use warnings;

package Server;

use IO::Socket::SSL;
use Storable qw(nfreeze);

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');
     my $packet = nfreeze(\%hash);
     $packet = pack("N/a*", $packet);
     print $client $packet;
     close($client);
     close($socket);
  }
}

package Client;

use IO::Socket::SSL;
use Storable qw(thaw);
use Data::Dumper;
use Safe;

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 $safe = new Safe;
  $safe->permit(qw(:default require));

  {
     no warnings 'once';
     $Storable::Deparse = 1;
     $Storable::Eval = sub { $safe->reval($_[0]) };
  }

  die if 4 != read($socket, my $buf, 4);
  my $len = unpack("N", $buf);
  die if $len != read($socket, $buf, $len);
  my $data = thaw($buf);
  close($socket);
  warn Dumper($data);
}

1;

if (my $pid = fork) {
  run Server;
  waitpid($pid,0);
} else {
  sleep 1;
  run Client;
}


Zum Testen das certs-Directory aus den Sourcen von CPAN:IO::Socket::SSL verwenden.

Das Beispiel funktioniert endlich einwandfrei und ich muss nicht auf
Storable verzichten. =)

Gruss,
opi\n\n

<!--EDIT|opi|1164786353-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread Hash Struktur senden: Client - Server