ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/eg/connect
(Generate patch)

Comparing AnyEvent/eg/connect (file contents):
Revision 1.1 by elmex, Thu May 15 08:37:55 2008 UTC vs.
Revision 1.2 by elmex, Sat May 24 17:06:10 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2use IO::Socket::INET; 2use IO::Socket::INET;
3use AnyEvent::Util; 3use AnyEvent::Socket;
4use AnyEvent::Handle; 4use AnyEvent::Handle;
5 5
6my $cv = AnyEvent->condvar; 6my $cv = AnyEvent->condvar;
7 7
8# Warning: IO::Socket::INET is going to do a BLOCKING
9# DNS lookup. Please see AnyEvent::Util::inet_aton how to
10# do nonblocking lookups!
11my $sock = IO::Socket::INET->new (
12 PeerAddr => "www.google.com:80",
13 Blocking => 0,
14) or die "Couldn't make socket: $!\n";
15
16my $hdl; 8my $hdl;
17 9
18# The $watchobj is just a guard that you have to keep 10my $watchobj = AnyEvent::Socket::tcp_connect ("www.google.com", 80, sub {
19# referenced until you are done with the connect.
20my $watchobj = AnyEvent::Util::connect ($sock, sub {
21 my ($sock) = @_; 11 my ($sock) = @_;
12
13 unless ($sock) {
14 warn "couldn't connect: $!";
15 return;
16 }
22 17
23 $hdl = 18 $hdl =
24 AnyEvent::Handle->new ( 19 AnyEvent::Handle->new (
25 fh => $sock, 20 fh => $sock,
26 on_eof => sub { print "received eof\n"; undef $hdl } 21 on_eof => sub { print "received eof\n"; undef $hdl }
33 print "Yay, got line: $line\n"; 28 print "Yay, got line: $line\n";
34 $cv->broadcast; 29 $cv->broadcast;
35 }); 30 });
36 31
37}, sub { 32}, sub {
38 warn "Got error on connect: $!\n"; 33 10 # the timeout
39 $cv->broadcast; 34});
40}, 10);
41 35
42$cv->wait; 36$cv->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines