hi
to ensure 1 instance of the application running using perl
i have a suggestion
1. pls open a specific port for locking
port must be greater than 10000 and
less than 60000
ex:
port 28004
2. so as long as the port is open, then, meaning there is still program running on that particular port
3. upon program exit, close the port
i have an ex code below:
hope this helps
its a class
how to use??
#----------------------------------------------------------------------------------------------
# Filename : PortLocker.pm
# Description : this is the package that will lock a specified port#
# Date : 08-10-2001
# Ver : ver1.1
# Author : bayugyug
#----------------------------------------------------------------------------------------------
package PortLocker;
use IO::Socket;
#--- set vars here
my $portlock = undef;
use constant PORT_LOCK => 28788;
#---
$| = 1;
my $VERSION = "1.01";
sub new
{
my $type = shift;
my $self = {};
$self->{'PORT'} = $_[0];
$self->{'PORT'} = PORT_LOCK if(int($self->{'PORT'}) <= 0);
bless $self, $type;
}
sub lock()
{
my $self = shift;
my $port = $self->{'PORT'};
$portlock = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => $port,
Proto => 'tcp',
Listen => 1
);
$self->debug("lock() :: port ($port) ok");
#--- return ok/fail
unless( $portlock )
{
return 0;
}
return 1;
}
sub unlock()
{
my $self = shift;
if(defined($portlock))
{
close($portlock);
}
How do you know port 52480 isn't already in use? This port lies within the Ephemeral Port Range of most modern Unix systems, and could at any time be used by an unrelated TCP session.