ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.1
Committed: Sun Apr 27 16:56:18 2008 UTC (16 years, 2 months ago) by elmex
Content type: application/x-troff
Branch: MAIN
Log Message:
added IO::AnyEvent as AnyEvent::Handle and AnyEvent::Socket, including tests

File Contents

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