Its a simple Perl socket server. It listens for commands on a given port, and executes an applescript if the command is ‘play’. There are two problems with the code below.
- Once the ‘play’ command is executed, both the client and the server are closed. (I want the server to continue listening for commands, be it ‘play’ or otherwise)
- The sockets don’t unbind. (I have to change the port number everytime I reuse it)
I’m using Terminal on a mac to execute the perl script, and telnet via terminal on another mac to trigger it.
#!/usr/bin/perl -w
use warnings;
use IO::Socket;
use Net::hostent;
$PORT = 8000;
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => 5,
Reuse => 1) or die "can't setup server" unless $server;
print "SERVER Waiting on port $PORT\n";
while ($client = $server->accept()) {
$client->autoflush(1);
print $client "Command:\r\n";
while (<$client>) {
if (/play/i) {
exec("osascript '/Users/user/Desktop/play.app'");
} else {
print $client "invalid command\r\n";
}
} continue {
print $client "Command: ";
} close $client;
}
You are using
exec. From perldoc -f execThis basically means that your socket closes, because the perl script exits. I assume this leaves the ports blocked because the socket is never explicitly or implicitly closed.