ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.7
Committed: Thu May 15 13:32:19 2008 UTC (16 years, 1 month ago) by elmex
Content type: application/x-troff
Branch: MAIN
Changes since 1.6: +53 -41 lines
Log Message:
added Util::listen function and removed Socket class. updated the listen test and MANIFEST. And added example script for listening.

File Contents

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