> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of Su,
> Yu (Eugene)
> Sent: Thursday, October 06, 2005 3:08 PM
> To: [email protected]
> Subject: RE: how to interact with an endless loop
>
> Hi All,
>
> I want to write a simple perl script to start an endless loop
> (for reading
> data from a periphery device, such as a measurement unit),
> but I also want
> to be able to stop it anytime. Right now I use "Ctrl-Alt-Del"
> to do it.
> There must be a better way. I has been trying to use open,
> pipe...not seem
> to understand how to use them.
>
Let me suggest another method. This method would have several
advantages. Within your loop, at certain times, you would need to poll
by calling a sub similar to this
#-----------------------------------------------------------------------
---#
# Check for a socket connection and process commands.
#
#-----------------------------------------------------------------------
---#
sub Connection()
{
use IO::Socket;
my $Status;
# Open a socket to listen and respond to connections.
$Log->WriteLog("Opening socket connection",0) if DEBUG;
my $Socket = IO::Socket::INET->new(LocalHost => $SocketServer,
Listen => 5,
LocalPort => $Port,
Proto => 'tcp',
Reuse => 1,
Timeout => $Interval);
unless($Socket) {$Log->WriteLog("A socket cannot be opened $!",1);
return(0)}
$Log->WriteLog("Waiting for a connection",0) if DEBUG;
# Process queue and program commands.
SESSION: while ($Comm = $Socket->accept())
{
my $Command;
$Status = 0;
$Log->WriteLog("Session established",0) if DEBUG;
while (<$Comm>)
{
chomp;
$Command = substr($_,0,3);
$Log->WriteLog("Monitor.pl received request $_",0);
print($Comm "Monitor.pl received request \'$_\'\n");
# End this connection if a 'end' command is received.
if ($Command eq 'end') {print($Comm "command complete\n");
last SESSION}
# Perform the Help subroutine if the 'help' command is
received.
if ($Command eq 'hel')
{
Help(1);
print($Comm "command complete\n");
next;
}
# Show all set options and their values.
if ($Command eq 'opt')
{
for (sort keys %Options) {print($Comm "Option -$_ =
$Options{$_}\n")}
print($Comm "command complete\n");
next;
}
# Exit this program if a 'term' command is received.
if ($Command eq 'ter') {print($Comm "command complete\n");
$Status = 1; last SESSION}
# Invalid command.
print($Comm "Invalid command\n");
print($Comm "command complete\n");
}
}
# Close session.
$Log->WriteLog("Session ended",0) if DEBUG;
close($Comm) if (defined $Comm);
# Close socket.
$Log->WriteLog("Closing socket connection",0) if DEBUG;
close($Socket)if (defined $Socket);
# Destroy object references.
$Comm = undef;
$Socket = undef;
exit(1) if ($Status);
return(1);
}
You then need a simple client program that will open a connection to
your endless-loop program and will send simple strings that will be
interpreted as commands. Advantages include that the socket connection
will time-out at the $Interval number of seconds and will then resume
your loop. You will be able to use the client program on another PC to
issue commands to the endless-loop program that is running on another
PC.
A simple client program:
#! C:/perl/bin/perl -w
# SocketClient.pl 05/17/2003.
# Declare pragmas.
use diagnostics;
use strict;
use warnings;
# Declare modules.
use FindBin qw($Bin);
use lib $Bin;
use IO::Socket;
my ($Port, $Server);
if (@ARGV) {$Server = shift}
else {$Server = 'target_server_name'}
if (@ARGV) {$Port = shift}
else {$Port = 80}
my ($Answer, $Socket);
SOCKET: while ()
{
# Attempt to open a socket connection to the specified server on the
# specified port.
print("Opening a socket connection to server $Server on port
$Port\n");
$Socket = IO::Socket::INET->new(PeerAddr => $Server, PeerPort =>
$Port, Proto => 'tcp');
# If the connection cannot be opened, wait ten seconds and try
again.
unless($Socket) {sleep(10); next}
# Process the connection.
while ()
{
# Prompt the user for a command string.
print("\nEnter command string>");
$_ = <STDIN>;
chomp;
# Exit this program based upon user input.
last SOCKET if ($_ eq 'exit');
# Write the entered command to the socket connection.
print($Socket "$_\n");
# Wait for and then process the information returned from the
server.
while ($Answer = <$Socket>)
{
chomp($Answer);
print("Accepted from server: $Answer\n");
# Exit this loop when the server replies that the submitted
# command has completed.
last if ($Answer eq 'command complete');
}
# Exit the socket connection if a end of terminate command was
entered.
last SOCKET if ($_ eq 'term' or $_ eq 'end');
}
}
# Close the socket connection.
close($Socket);
exit(1);
__END__
Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO
- USA Central Time Zone
636-755-2652 fax 636-755-2503
[EMAIL PROTECTED]
www.nisc.coop
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs