http://www.perlmonks.org?node_id=1026977


in reply to Asynchronous HTTP Request using specific ports

I tried and the following example works for me:

use strict; use warnings; use IO::Socket::INET; my $sock1 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7777, LocalPort => 6666, ReuseAddr => 1, ) or die $!; my $sock2 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7778, LocalPort => 6666, ReuseAddr => 1, ) or die $!; say $sock1 "foo"; say $sock2 "bar"; sleep 100;
Probably it's something specific to your system

Replies are listed 'Best First'.
Re^2: Asynchronous HTTP Request using specific ports
by Adamba (Sexton) on Apr 04, 2013 at 12:09 UTC

    If you try it in synchronous way, it will work.
    That I know for sure. I want to do it async...

      If I try it asynchronous way it still works:

      use 5.010; use strict; use warnings; use AnyEvent; use AnyEvent::Handle; use IO::Socket::INET; my $sock1 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7777, LocalPort => 6666, ReuseAddr => 1, ) or die $!; my $ah1; $ah1 = AnyEvent::Handle->new( fh => $sock1, on_read => \&send_echo ); my $sock2 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7778, LocalPort => 6666, ReuseAddr => 1, ) or die $!; my $ah2; $ah2 = AnyEvent::Handle->new( fh => $sock2, on_read => \&send_echo ); AE::cv->recv; sub send_echo { my $handle = shift; $handle->push_write( substr $handle->{rbuf}, 0, length $handle->{r +buf}, '' ); }

      PS: perhaps you should show us example that doesn't work for you

        That doesn't seems to work:

        #!/usr/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; use AnyEvent::Socket qw( tcp_connect ); use Socket; open my $fh, "<", "host_list.txt"; my @hosts = <$fh>; close $fh; chomp @hosts; my @ports = qw/65011 65021 65012 65022 65031 65038 65032 65039/; my $cv = AnyEvent->condvar; foreach my $port (@ports) { foreach my $host (@hosts) { print "[*] $host:$port\n"; $cv->begin; check_http($port, $host); } } $cv->recv; sub check_http { my ($port_number, $host) = @_; my $ret_val; $host = 'http://' . $host; http_get $host, persistent => 0, on_prepare => sub { my ($fh) = @_; bind($fh, sockaddr_in($port_number, INADDR_ANY)) or print "bind: $!\n"; 15 }, sub { my ($body, $hdr) = @_; if ($hdr->{Status} =~ /^2/) { print "Seems OK! (:\n"; } else { print "ERROR, $hdr->{Status} $hdr->{Reason}\n"; } print "[X] $host:$port_number\n"; $cv->end; }; }