Hello Monks! I've had a number of posts recently about a script I'm working on and I'm back again still stuck on a problem incrementing variables.
The following is part of a script which handles incoming socket connections, processes them (currently to Log file) then sends a formatted response message back to the server (an "ACK/NAK" type handshake method). My goal is to have our $stat_rsnr_seq increment for each connection after it is accepted as shown in the STAT elsif statement. For reference, the initConnection() handles the client socket connection, the LogIt sub handles logging to a text file, sendRequest handles the socket send() functionality, and recv() handles the socket recv() functionality. My problem is that a client is accepted, data is received by this script matching the STAT pattern and the response is sent to the client as it should. HOWEVER the increment never appears to take place and is always '00001' even though I need it to go up to '99999' then back to '00001' if it exceeds '99999'. What am I missing? I'm assuming it's something trivial that I'm just overlooking. Thank you for any assistance!
#!/usr/bin/perl -w
use warnings;
use IO::Socket::INET;
use POSIX qw( WNOHANG );
# ... other variable initializations
our $stat_rsnr_seq = '00001';
# ... Irrelevant listening port information and data handling subs
while () { # forever
while ($handle = $listener->accept()) {
print "A client has connected!\n";
my $pid = fork();
die "Cannot fork: $!" unless defined($pid);
do {
$pid = waitpid(-1, WNOHANG);
} while $pid > 0; # end do { $pid = waitpid(-1, WNOHANG); }
if ($pid == 0) { # child process
my $accepted_message = &recv();
&LogIt("Received message from EMS: $accepted_message");
# PING response
if ($accepted_message =~ /00000RSNR00000/) {
# RSNR methods/RESP response/wait on STAT
}
elsif ($accepted_message =~ /ADD00200/) {
# ADD methods/RESP response/wait on STAT
}
elsif ($accepted_message =~ /STAT00025/) {
# log message
&LogIt("Message received: $accepted_message");
##############################################
# Transmit to server
$handle = initConnection($host, $port);
&LogIt("init_Connection - $handle");
if (!$handle) {
&LogIt("Cannot connect to port $port: $!");
} # end of (!$handle) if statement
else {
my $msglength = 20 + 5; # Item ID + Status Code
my $request_msg = $stat_rsnr_seq."RSNR"."00005"."00000";
$stat_rsnr_seq++;
&sendRequest($request_msg);
my $resp_msg = &recv();
if ($resp_msg =~ /RESP0000500000/) {
&LogIt("Success: $accepted_message, $resp_msg");
} # end of if ($resp_msg =~ /RESP0000500000/)
elsif ($resp_msg =~ /RESP0000501000/) {
&LogIt("Invalid transaction code: $accepted_message, $resp_msg");
} # end of elsif ($resp_msg =~ /RESP0000501000/)
elsif ($resp_msg =~ /RESP0000501001/) {
&LogIt("Retry: $accepted_message, $resp_msg");
} # end of elsif ($resp_msg =~ /RESP0000501001/)
elsif ($resp_msg =~ /RESP0000501002/) {
&LogIt("Invalid sequence number: $accepted_message, $resp_msg");
} # end of elsif ($resp_msg =~ /RESP0000501002/)
elsif ($resp_msg =~ /RESP0000500020/) {
&LogIt("Invalid length: $accepted_message, $resp_msg");
} # end of elsif $resp_msg =~ (/RESP0000500020/)
else {
# Try again!
$handle = initConnection($host, $port);
&sendRequest($request_msg);
my $resp_msg = &recv();
if($resp_msg !~ /RESP0000500000/){
&LogIt("Error with message: $accepted_message, $resp_msg");
}
}
$handle->close();
} # end of (!$handle) else statement
exit(0);
} # end of elsif ($accepted_message =~ /STAT00025/)
} # end if ($pid == 0)
} # end while ($handle = $listener->accept())
} # end while ()