Hi
I'm a very new perl programmer and I'm writing my first perl code, however I'm having a few problems. The first was related with my actual problem, I have a code that connects to different ports and check for SOCKS, however it took very long to return if the server was not a SOCKS server, also I need to use threads to make it less slow and scan multiple services at the same time.
The suggestion was to use alarm() and it worked fine.
http://www.perlmonks.org/index.pl?node_id=838963
Well, it worked fine in a single test script without threads, when I start to use threads on my real code all get messed up.
I get all the time a error "Alarm clock" and the process die / killed, however I never defined a entry with this output "Alarm clock".
My code is like that:
#!/usr/bin/perl
use Socket;
use IO::Socket;
use Net::SOCKS;
use threads;
use threads::shared;
my ($host, $port) = @ARGV;
mfunc();
$port = 1080;
mfunc();
$port = 80;
mfunc();
for my $lthrd (1..7){
my $nthrd = threads->new(\&mfunc);
foreach (threads->list){
$_->join;
}
}
$port = 22;
mfunc();
sub mfunc{
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required
alarm 7;
my $sock = new Net::SOCKS(socks_addr => $host, socks_port => $port, pr
+otocol_version => 4);
my $rsocks = $sock->connect(peer_addr => 'www.google.com', peer_port =
+> 80);
my $status = $sock->param('status_num');
if ($status == SOCKS_OKAY){
print "\nWorked $host:$port\n";
$sock->close();
}
alarm 0;
};
die if $@ && $@ ne "alarm\n"; # propagate errors
if ($@) {
print("\nTimeout $host:$port\n");
}
}
If you try you will get
./test.pl www.google.com 1080
Test 01
Timeout www.google.com:1080
Timeout www.google.com:3128
Alarm clock
It proves that the code works fine out of the thread, but on the threads it get completely crazy.
I saw this possible suggestion to fix the problem:
http://www.mail-archive.com/perl5-porters@perl.org/msg89360.html
However, now the code gives this error and back to be very slow again, probable when the first alarm() happen it get unhanded and never more work.
SIGALRM handler "a" not defined.
I can't believe there is no way to fix it in perl, can someone please help me in fix this code (examples are very welcome) to work with the threads and with the alarm()? Or any other approach that make the code fast and not waiting 5 minutes per connection that is not SOCKS...
Thank you
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.