Perl 5.8 threads and sockets problem

abez abez at abez.ca
Sun Feb 23 22:35:24 CST 2003



Here's what I'd do:

use IO::Socket qw(:DEFAULT :crlf);

my @thr = ();
my @Data = (
	 { 'UID' => "abez", 'SVR' => "lycos.com", 'PORT' => "80",
'LOGIN' => "abez", 'PASS' => "******" },
	 { 'UID' => "casper", 'SVR' => "www.metafilter.com", 'PORT' => "80",
'LOGIN' => "casper", 'PASS' => "*****" },
);
my $i = 0;

Startup();

foreach my $account (@Data) {
	$str = " rm -f $account->{UID}.acc ";
	`$str`;
}

$| = 0;
sub Startup {
	for (;;) {
      #Win32::Sleep(4000);
		foreach my $account (@Data) {
			#if (-f $account->{'UID'}.".acc") {
 	 		#	next;
  			#}
			print "Forking $account->{UID}\n";
			my $pid = fork();
			if ($pid==0) { #child
				print "Child $account->{UID}\n";
				go($account);
				exit();
			}
                }
		sleep(10);
        }
  }




sub go($$$$$) {
	my $account = shift;

my $pop3C = IO::Socket::INET->new(Proto => 'tcp', PeerAddr =>
	$account->{SVR},
	PeerPort => $account->{PORT} , Timeout => 60)
	or pop3CError ($account->{SVR},$account->{PORT});
open(X,">".$account->{UID}.".acc");
close(X);
print "in $account->{UID}\n";
print $pop3C "GET /\n";
my @out = <$pop3C>;
print join("", at out[0..10]),"\n";
$pop3C->close;

$pop3C = undef;
unlink($_[1].".acc");
}

sub pop3CError ($$) {

print "Cannot connect to the the Pop3 server:$_[0], port:$_[1]\n";
exit;

}



On Sun, 23 Feb 2003, Jeremy Aiyadurai wrote:

> hi All,
>
> Thanks for your help so far.
>
> I tried threads->create, it made no difference,
> as mentioned in the manpage, "The new() method is an alias for create(). "
>
> Your Help is much appreciated,
> thanks
>
> Jeremy A.
>
> >From: abez <abez at abez.ca>
> >To: Jeremy Aiyadurai <jeremygwa at hotmail.com>
> >CC: victoria-pm at pm.org
> >Subject: Re: Perl 5.8 threads and sockets problem
> >Date: Sun, 23 Feb 2003 18:38:16 -0800 (PST)
> >
> >
> >First of all try threads->create, rather than new. Other than that I'm too
> >dizzy to really help right now (flu).
> >
> >On Sun, 23 Feb 2003, Jeremy Aiyadurai wrote:
> >
> > >
> > > Hello all,
> > >
> > > I am new to the group.
> > > I have a problem involving sockets and threads.
> > >
> > > Basically, I want to be able to do two or more pop3 sessions
> >simultaniously
> > > using a list of different pop3 accounts.
> > > my problem is, I can logon to the first account, but when
> > > it comes to the second account's turn (in the second thread), the socket
> > > cannot be created.
> > >
> > > I am new to using threads and have little knowledge of sockets.
> > >
> > > Your Help is much appreciated,
> > >
> > > Thanks,
> > >
> > > Jeremy A.
> > >
> > > Below is my test script I am having trouble with.
> > >
> > >
> > > #-----------------------------------------------------
> > > use threads;
> > > use Win32;
> > > use IO::Socket qw(:DEFAULT :crlf);
> > >
> > >
> > > my @thr;
> > > my @Data;
> > > $Data[0] = { 'UID' => "Jeremy", 'SVR' => "mail.host.net", 'PORT' =>
> >"110",
> > > 'LOGIN' => "account1", 'PASS' => "******" };
> > > $Data[1] = { 'UID' => "Dave", 'SVR' => "mail.bost.com", 'PORT' => "110",
> > > 'LOGIN' => "account2", 'PASS' => "*****" };
> > > my $i = 0;
> > >
> > > sub Startup {
> > >     while (1) {
> > >       Win32::Sleep(4000);
> > >         foreach my $account (@Data)
> > >         {
> > >                 if(!open(T,"".$account->{'UID'}.".acc"))
> > >                 {
> > >   		Win32::Sleep(40);
> > >   		if($i != 0)
> > >   		{
> > >   		 $i = $i + 1;
> > >   		}
> > >   		$thr[$i] =
> > >
> >threads->new(\&go,$account->{'UID'},$account->{'SVR'},$account->{'PORT'},$account->{'LOGIN'},$account->{'PASS'});
> > > 		$thr[$i]->join;
> > >                 }else
> > >                 {
> > >                         close(T);
> > >                 }
> > >                 $i = $i - 1;
> > >
> > >         }
> > >   }
> > >   @AC = -1;
> > > }
> > >
> > > Startup();
> > >
> > >
> > >
> > > sub go($$$$$) {
> > > print "$_[0],$_[1],$_[2],$_[3],$_[4],$_[5],$_[6]\n";
> > >
> > > my $pop3C = IO::Socket::INET->new(Proto => 'tcp', PeerAddr => "$_[1]",
> > > PeerPort => $_[2], Timeout => 60) or pop3CError ($_[1],$_[2]);
> > > open(X,">".$_[1].".acc");
> > > close(X);
> > > print "in $_[1]\n";
> > > sleep(1);
> > > pop3close($pop3C);
> > > $pop3C = undef;
> > > unlink($_[1].".acc");
> > > }
> > >
> > >
> > > sub pop3CError ($$) {
> > >
> > > print "Cannot connect to the the Pop3 server:$_[0], port:$_[1]\n";
> > > exit;
> > >
> > > }
> > >
> > >
> > > sub pop3close ($) {
> > > 	if ($_[0]) {
> > > 	 shutdown ($_[0], 2);
> > > 	}
> > > }
> > > #-----------------------------------------------------
> > >
> > >
> > >
> > >
> > >
> > > _________________________________________________________________
> > > The new MSN 8: smart spam protection and 2 months FREE*
> > > http://join.msn.com/?page=features/junkmail
> > >
> >
> >--
> >abez ------------------------------------------
> >http://www.abez.ca/ Abram Hindle (abez at abez.ca)
> >------------------------------------------ abez
>
>
> _________________________________________________________________
> Add photos to your messages with MSN 8. Get 2 months FREE*.
> http://join.msn.com/?page=features/featuredemail
>

-- 
abez ------------------------------------------
http://www.abez.ca/ Abram Hindle (abez at abez.ca)
------------------------------------------ abez



More information about the Victoria-pm mailing list