ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.4
Committed: Fri May 2 09:06:56 2008 UTC (16 years, 2 months ago) by root
Content type: application/x-troff
Branch: MAIN
Changes since 1.3: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 #!/opt/perl/bin/perl
2 root 1.2
3 elmex 1.1 use strict;
4     use Test::More tests => 2;
5 root 1.2 use AnyEvent::Impl::Perl;
6 elmex 1.1 use AnyEvent;
7     use AnyEvent::Socket;
8    
9     my $lbytes;
10     my $rbytes;
11    
12     my $cv = AnyEvent->condvar;
13    
14     my $lsock =
15     AnyEvent::Socket->new (
16     Listen => 1,
17     LocalPort => 32391,
18     ReuseAddr => 1,
19     );
20 root 1.4
21 elmex 1.1 my $ae_sock =
22     AnyEvent::Socket->new (
23 root 1.3 PeerAddr => "127.0.0.1:32391",
24 elmex 1.1 on_connect => sub {
25     my ($ae_sock, $error) = @_;
26     if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
27    
28     print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n";
29    
30     $ae_sock->on_read (sub {
31     my ($ae_sock) = @_;
32     $rbytes = $ae_sock->rbuf;
33     });
34    
35     $ae_sock->write ("TEST\015\012");
36     }
37     );
38    
39     $ae_sock->on_eof (sub { $cv->broadcast });
40    
41     $lsock->on_accept (sub {
42     my ($lsock, $cl, $paddr) = @_;
43    
44     unless (defined $cl) {
45     diag "accept failed: $!";
46     return;
47     }
48    
49     $cl->read (6, sub {
50     my ($cl, $data) = @_;
51     $lbytes = $data;
52     $cl->write ("BLABLABLA\015\012");
53     });
54     });
55    
56     $cv->wait;
57    
58     is ($lbytes, "TEST\015\012", 'listening end received data');
59     is ($rbytes, "BLABLABLA\015\012", 'connecting received response');