UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » Perl with threads and alarm() is possible?

#1 2010-05-10 04:51 PM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

#!/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, protocol_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");

 }

}

Offline

#2 2010-05-10 07:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Perl with threads and alarm() is possible?

Offline

#3 2010-05-10 11:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Perl with threads and alarm() is possible?

Or switch to multiple processes instead of threads.

But yeah, if you're going to open many connections anyway I'd switch to
non-blocking sockets with multiplexing too.

Offline

#4 2010-05-11 12:08 AM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

Hi RobSeace and i3839

The bad news is that appear that select() doesn't work with Net::SOCKS in perl. Too bad!

How could make this process with multiple process instead threads? I mean, because in threads we share data, in multiple process (fork) I will have different instances repeating the same task, which for sure will not make my program posted on the thread to be fast as it's with threads.

Thank you

Offline

#5 2010-05-11 01:22 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Perl with threads and alarm() is possible?

Umm, there is zero data sharing after thread creation in the code you posted.
Doing the same, but with fork should be hardly slower. You can still kill all
other childs if one of them gets a connection and you don't want the rest to do
anything.

There must be way to fish out the underlaying file descriptor/socket from
Net::SOCKS, so if you have a select or poll in perl it should work.

Offline

#6 2010-05-12 05:16 PM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

Hi i3839

Thanks for the answer, I'm researching.

Offline

  • Index
  • » Threads
  • » Perl with threads and alarm() is possible?

Board footer

Powered by FluxBB