UPDATE:
Since English is my second language I think I did not express myself correctly, as
webfiend put it what I am looking is that how to use
Net::FTP::AutoReconnectinside my program, what structure like IF, WHILE, DO WHILE, I need to use to make the program reconnect, and how to catch the result when trying to connect to the server and fails, for example :
use Net::FTP;
use strict;
{
my $pollingInterval = 1;
my $directoryToMonitor = "C:/testdir";
my %oldFileList = ();
my %currentFileList = ();
# we start by getting the file list, considered to be the "old" list
#
getFileList($directoryToMonitor, \%oldFileList);
for (;;) {
sleep($pollingInterval);
# get the current file list and compare it to the old file list
# if the lists differ, the current file list becomes the old file
+list
# and we FTP the files
#
getFileList($directoryToMonitor, \%currentFileList);
if ( fileListsDiffer(\%oldFileList, \%currentFileList) ) {
copyFileList(\%oldFileList, \%currentFileList);
#dumpFileList(\%oldFileList);
# the FTP commands should go here instead of the dumpFileList()
+function above
my @transferFileList = glob "m*.*";
my @fileList = glob "m*.*";
foreach my $file (@fileList) {
chomp($file);
if ( $file =~ /.+\.\d{1,}/ ) { # one or more characters, a '.' foll
+owed by one or more digits
print "Iniciando transmision de datos...\n";
print STDOUT "<Nombre valido del Archivo>" . $file . "\n";
my $filesize = -s $file;
print "Size: $filesize\n";
my $server = "124.32.54.59";
my $username = "saai";
my $pass = "saaisi0";
my $ftp;
print "Connecting to $server..\n";
# Set up connection
$ftp = Net::FTP->new( $server, Passive => 1, Debug => 0 ) o
+r die $@;
print "..authenticating..\n";
# Log in...
$ftp->login( $username, $pass ) or die $ftp->message;
print "..done!\n";
$ftp->cwd('Envio') or die $ftp->message;
print "Directorio actual es :\n";
print $ftp->pwd (), "\n";
$ftp->ascii();
$ftp->put("$file") or die $ftp->message;
my $tamano = $ftp->size($file);
print "el tamano del archivo es : $tamano\n";
print "Comprobando el envio de los archivos\n";
if ($filesize = $tamano) {
print "\nSON DEL MISMO TAMANO\n";
}
else {
print "ooooopppssss!!!! intentando enviarlo de nuevo";
}
print "Logging out..";
#or die $ftp->message;
$ftp->quit;
print "..done!\n\n\n";
}
else {
print STDOUT "<Nombre Invalido del Archivo> " . $file . "\n\n\n";
}
}
for (@transferFileList) {
#print "$_\n";
system ("del",$_);
}
}
}
}
# Basically prints the contents of a hash.
#
sub dumpFileList
{
my $list = shift;
my @k = sort(keys(%$list));
for (my $i=0; $i<=$#k; $i++) {
print $k[$i] . " " . $list->{$k[$i]} . "\n";
}
print "\n";
}
# Get a list of the files along with there modification times
# from the specified directory. The results are place in the
# specified hash where the key is the file name and the value
# is that file's modification time.
#
sub getFileList
{
my $directory = shift;
my $hash = shift;
%$hash = ();
opendir(DIR,$directory) || die "Can not open $directory";
my @files = grep(!/^\.\.?$/, readdir(DIR));
closedir(DIR);
for (my $i=0; $i<=$#files; $i++) {
$hash->{$files[$i]} = (stat("$directory/$files[$i]"))[9];
}
}
# See if two hashes of files differ. The hashes are considered differ
+ent if one of the
# following occurrs:
#
# 1. The hashes have different number of elements.
# 2. The new hash is missing a key that is in the old hash.
# 3. For a given key, the value in the old hash differs from the val
+ue in the new hash.
#
sub fileListsDiffer
{
my $oldList = shift;
my $newList = shift;
my @oldKeys = (keys(%$oldList));
my @newKeys = (keys(%$newList));
if ($#newKeys != $#oldKeys) {
return 1;
}
foreach my $key (@oldKeys) {
if ( ! defined($newList->{$key}) || $newList->{$key} ne $oldList->
+{$key} ) {
return 1;
}
}
return 0;
}
# Copies one hash to another. The destination has ($to below) is empt
+ied first.
#
sub copyFileList
{
my $to = shift;
my $from = shift;
%$to = ();
foreach my $key (keys(%$from)) {
$to->{$key} = $from->{$key};
}
}
Where in this program I can use reconnect or how to use it?
thanks!
_____________________________________________________________
Dear monks!!!
I come to you dear monks in seek of knowledge, I need in my program to have a reconnect feature in case of the FTP fails
it try to reconnect again but I dont know how to implement it on my program can someone please help me ?
thank you dear monks