ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.2
Committed: Sun Apr 27 19:36:55 2008 UTC (16 years, 2 months ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-3_3
Changes since 1.1: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/perl/bin/perl
2
3 use strict;
4 use Test::More tests => 2;
5 use AnyEvent::Impl::Perl;
6 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 my $ae_sock =
21 AnyEvent::Socket->new (
22 PeerAddr => "localhost:32391",
23 on_connect => sub {
24 my ($ae_sock, $error) = @_;
25 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
26
27 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n";
28
29 $ae_sock->on_read (sub {
30 my ($ae_sock) = @_;
31 $rbytes = $ae_sock->rbuf;
32 });
33
34 $ae_sock->write ("TEST\015\012");
35 }
36 );
37
38 $ae_sock->on_eof (sub { $cv->broadcast });
39
40 $lsock->on_accept (sub {
41 my ($lsock, $cl, $paddr) = @_;
42
43 unless (defined $cl) {
44 diag "accept failed: $!";
45 return;
46 }
47
48 $cl->read (6, sub {
49 my ($cl, $data) = @_;
50 $lbytes = $data;
51 $cl->write ("BLABLABLA\015\012");
52 });
53 });
54
55 $cv->wait;
56
57 is ($lbytes, "TEST\015\012", 'listening end received data');
58 is ($rbytes, "BLABLABLA\015\012", 'connecting received response');