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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines