File: connect

package info (click to toggle)
libanyevent-perl 7.170-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,964 kB
  • sloc: perl: 6,646; sh: 113; makefile: 12
file content (36 lines) | stat: -rwxr-xr-x 664 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/opt/perl/bin/perl
use IO::Socket::INET;
use AnyEvent::Socket;
use AnyEvent::Handle;

my $cv = AnyEvent->condvar;

my $hdl;

my $watchobj = AnyEvent::Socket::tcp_connect ("www.google.com", 80, sub {
   my ($sock) = @_;

   unless ($sock) {
      warn "couldn't connect: $!";
      return;
   }

   $hdl =
      AnyEvent::Handle->new (
         fh => $sock,
         on_eof => sub { print "received eof\n"; undef $hdl }
      );

   $hdl->push_write ("GET / HTTP/1.0\015\012\015\012");

   $hdl->push_read (line => sub {
      my ($hdl, $line) = @_;
      print "Yay, got line: $line\n";
      $cv->broadcast;
   });

}, sub {
   10 # the timeout
});

$cv->wait;