package PerlSvc; use strict; use warnings; use Time::HiRes qw( sleep ); use IO::Socket; use TextLogger; # the short name by which your service will be known (cannot be 'my') our $Name = "pmtimesrv"; # the display name. This is the name that the Windows Control Panel # will display (cannot be 'my') our $DisplayName = "PerlMonks Time Server"; # the startup routine is called when the service starts sub Startup { # Start listening to our server socket my $server = IO::Socket::INET->new(LocalPort => 64100, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) # or SOMAXCONN or die "Couldn't be a Time server on port 64100 : $@\n"; my $oldfh = select($server); $| = 0; select($oldfh); my $temp = 1; ioctl ($server, 0x8004667E, \$temp); # set non-blockin # Make a new logger my $logfilename = 'C:\Programme\PerlMonks\TimeServer.log'; my $logger = new TextLogger(logfile => $logfilename, appname => 'TimeServer'); $logger->log("Starting to serve timestamps"); my $alivecount = 36000; while (ContinueRun()) { $alivecount--; if(!$alivecount) { $alivecount = 36000; $logger->alive(); } my $client = $server->accept(); # see if there are new clients waiting if(!defined($client)) { # ok, no client waiting, sleep a short time and retry sleep(0.1); next; } $logger->log("Connection from: " . $client->peerhost); print $client getDateTime(); close $client; } $logger->log("Someone has set up us the bomb!"); # mandatory AYBABTU quote } sub Install { # add your additional install messages or functions here print "The $Name Service has been installed.\n"; print "Start the service with the command: net start $Name\n"; } sub Remove { # add your additional remove messages or functions here print "The $Name Service has been removed.\n"; } sub Help { # add your additional help messages or functions here print "$Name Service -- use '--install' to install, '--remove' to uninstall. Or run interactive.\n"; } sub Interactive { Startup(); } # ---- HELPERS ---- sub doPad { my ($val, $len) = @_; while(length($val) < $len) { $val = "0$val"; } return $val; } sub getDateTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time()); return doPad($mday,2) . "-" . doPad($mon+1,2). "-" . ($year + 1900) . " " . doPad($hour,2) . ":" . doPad($min, 2) . ":" . doPad($sec, 2) . "\n"; }