#!/usr/bin/perl #use strict; use warnings; use IO::Socket; use threads; use Thread::Queue; use Mysql; # use Proc::Daemon; # Proc::Daemon::Init; # use Proc::PID::File; # my $pid = Proc::PID::File->running(name => "foo"); #get the port to bind to or default to 8000 my $port = $ARGV[0] || 8000; my $queue = Thread::Queue -> new; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; #create the listen socket my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 1, Proto => 'tcp', Reuse => 1); #make sure we are bound to the port die "Cant't create a listening socket: $@" unless $listen_socket; warn "Server ready. Waiting for connections on $port ... \n"; #wait for connections at the accept call while (my $connection = $listen_socket->accept) { threads->create ("read_data", $queue, $connection)->detach; print $connection; } sub read_data { # accept data from the socket and put it on the queue my ($queue, $socket) = @_; while (<$socket>) { print "$_"; // Process data from connected devices $queue -> enqueue(time." $_"); } close $socket; }