ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
(Generate patch)

Comparing AnyEvent/t/handle/04_listen.t (file contents):
Revision 1.8 by elmex, Thu May 15 13:50:23 2008 UTC vs.
Revision 1.9 by root, Sat May 17 21:08:05 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2
2use strict; 3use strict;
4
3use AnyEvent::Impl::Perl; 5use AnyEvent::Impl::Perl;
4use AnyEvent::Handle; 6use AnyEvent::Handle;
5use AnyEvent::Util; 7use AnyEvent::Util;
6use IO::Socket::INET;
7 8
8my $lbytes; 9my $lbytes;
9my $rbytes; 10my $rbytes;
10 11
11print "1..2\n"; 12print "1..2\n";
12 13
13my $cv = AnyEvent->condvar; 14my $cv = AnyEvent->condvar;
14 15
15my $sock = IO::Socket::INET->new ( 16my $hdl;
16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost', 17my $port;
17) or die "Couldn't make socket: $!\n";
18 18
19my $hdl; 19my $w = AnyEvent::Util::tcp_server undef, undef,
20 sub {
21 my ($fh, $host, $port) = @_;
20 22
21my $w = AnyEvent::Util::listen ($sock, sub {
22 my ($cl, $claddr) = @_;
23 $hdl = AnyEvent::Handle->new (fh => $cl, on_eof => sub { $cv->broadcast }); 23 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
24 24
25 $hdl->push_read_chunk (6, sub { 25 $hdl->push_read_chunk (6, sub {
26 my ($hdl, $data) = @_; 26 my ($hdl, $data) = @_;
27 27
28 if ($data eq "TEST\015\012") { 28 if ($data eq "TEST\015\012") {
29 print "ok 1 - server received client data\n"; 29 print "ok 1 - server received client data\n";
30 } else { 30 } else {
31 print "not ok 1 - server received bad client data\n"; 31 print "not ok 1 - server received bad client data\n";
32 } 32 }
33 33
34 $hdl->push_write ("BLABLABLA\015\012"); 34 $hdl->push_write ("BLABLABLA\015\012");
35 }); 35 });
36 }, sub {
37 ($port) = Socket::unpack_sockaddr_in getsockname $_[0];
36 38
37}, sub { 39 0
38 warn "error on accept: $!"; 40 };
39 $cv->broadcast; 41
40});
41 42
42my $clhdl; 43my $clhdl;
43my $wc = AnyEvent::Util::tcp_connect ($sock->sockhost, $sock->sockport, sub { 44my $wc = AnyEvent::Util::tcp_connect localhost => $port, sub {
44 my ($clsock) = @_; 45 my ($fh) = @_;
46
45 $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast }); 47 $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
46 48
47 $clhdl->push_write ("TEST\015\012"); 49 $clhdl->push_write ("TEST\015\012");
48 $clhdl->push_read_line (sub { 50 $clhdl->push_read_line (sub {
49 my ($clhdl, $line) = @_; 51 my ($clhdl, $line) = @_;
50 52
54 print "not ok 2 - client received bad response\n"; 56 print "not ok 2 - client received bad response\n";
55 } 57 }
56 58
57 $cv->broadcast; 59 $cv->broadcast;
58 }); 60 });
59}, sub { 61};
60 warn "couldn't connect: $!";
61 $cv->broadcast;
62}, 10);
63 62
64$cv->wait; 63$cv->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines