Hi Guys,
Was wondering if you can help me. I am relative new to perl scriptting, I have the following script (simplified a bit as it is work related).
Code:
#!/usr/bin/perl -w
#
my $sock = new IO::Socket::INET (
LocalHost => '192.168.0.1',
LocalPort => '8888',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $sock;
# Open socket
start:
$alive=1;
my $new_sock = $sock->accept();
print $new_sock "WELCOME 100";
# Command listner
while(<$new_sock>) {
$m=$_;
$m =~ s/\n|\r//g;
if ($m eq ".PART.") {
print $new_sock "CLOSE 101 Disconnecting. Good bye!";
$alive=0;
}
last if ($m eq ".PART.");
# Call shell script with appropriate action
system "/bin/bash -x /root/scripts/functions $m >&1";
}
close($new_sock);
print "Closed socket and will restart it now";
goto start
I use goto start at the end to reopen the socket and listen for a new connmection after a user disconnects.
Code:
# Keep alive thread
my $thrKL = threads->create(\&keepalive);
$thrKL->detach();
sub keepalive{
while ($alive == 1) {
print $new_sock "Keep alive!\n";
sleep(1);
}
}
However if I add my keepalive thread which looks like the above code, then the script will exit after a .PART. is received and it will not even print the "Closed socket and will restart it now" line. What am I doing wrong?
Thanks!