#!/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 ()