Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Ah, now I see the problem. I couldn't create exact test conditions, so simplified script a bit. There were two problems: first, you forgot to set SO_REUSEADDR, second, AnyEvent::HTTP under the hood tried to create second connection to the server with the same source/destination ports, this of course couldn't work (thanks to strace for the help), I fixed it by setting persistent to 1. So here's the script that worked for me, hope it will help:

#!/usr/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; use AnyEvent::Socket qw( tcp_connect ); use Socket; my @hosts = qw(host1 host2); my @ports = qw(80); my $cv = AnyEvent->condvar; foreach my $port (@ports) { foreach my $host (@hosts) { print "[*] $host:$port\n"; $cv->begin; check_http(1111, $host); } } $cv->recv; sub check_http { my ($port_number, $host) = @_; my $ret_val; $host = 'http://' . $host; http_get $host, persistent => 1, on_prepare => sub { my ($fh) = @_; setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, 1) or warn "Coul +dn't set SO_REUSEADDR"; 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; }; }

In reply to Re^5: Asynchronous HTTP Request using specific ports by zwon
in thread Asynchronous HTTP Request using specific ports by Adamba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found