1 |
elmex |
1.1 |
#!/opt/perl/bin/perl |
2 |
|
|
use IO::Socket::INET; |
3 |
|
|
use AnyEvent::Util; |
4 |
|
|
use AnyEvent::Handle; |
5 |
|
|
|
6 |
|
|
my $cv = AnyEvent->condvar; |
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! |
11 |
|
|
my $sock = IO::Socket::INET->new ( |
12 |
|
|
PeerAddr => "www.google.com:80", |
13 |
|
|
Blocking => 0, |
14 |
|
|
) or die "Couldn't make socket: $!\n"; |
15 |
|
|
|
16 |
|
|
my $hdl; |
17 |
|
|
|
18 |
|
|
# The $watchobj is just a guard that you have to keep |
19 |
|
|
# referenced until you are done with the connect. |
20 |
|
|
my $watchobj = AnyEvent::Util::connect ($sock, sub { |
21 |
|
|
my ($sock) = @_; |
22 |
|
|
|
23 |
|
|
$hdl = |
24 |
|
|
AnyEvent::Handle->new ( |
25 |
|
|
fh => $sock, |
26 |
|
|
on_eof => sub { print "received eof\n"; undef $hdl } |
27 |
|
|
); |
28 |
|
|
|
29 |
|
|
$hdl->push_write ("GET / HTTP/1.0\015\012\015\012"); |
30 |
|
|
|
31 |
|
|
$hdl->push_read_line (sub { |
32 |
|
|
my ($hdl, $line) = @_; |
33 |
|
|
print "Yay, got line: $line\n"; |
34 |
|
|
$cv->broadcast; |
35 |
|
|
}); |
36 |
|
|
|
37 |
|
|
}, sub { |
38 |
|
|
warn "Got error on connect: $!\n"; |
39 |
|
|
$cv->broadcast; |
40 |
|
|
}, 10); |
41 |
|
|
|
42 |
|
|
$cv->wait; |