Time Server with Socket : Socket « Network « Perl






Time Server with Socket

    

#!/bin/perl  -Tw
require 5.6;
use strict;
use Socket;
use FileHandle;

#Usage: timeserver
my($this, $now);
my $port = shift || 99999;

$this = pack('Sna4x8', AF_INET, $port, "\0\0\0\0");
print "Port = $port\n";
my $prototype = getprotobyname('tcp');
socket(SOCKET, PF_INET, SOCK_STREAM, $prototype) || die "socket: $!\n";
print "Socket ok.\n";

bind(SOCKET, $this) || die "bind: $!\n";
print "Bind ok.\n";

listen(SOCKET, SOMAXCONN) || die "connect: $!\n";
print "Listen ok.\n";

COMM_SOCKET->autoflush;
SOCKET->autoflush;

while(1){
     print "In loop.\n";
     accept(COMM_SOCKET, SOCKET) || die "$!\n";
     print "Accept ok.\n";
     $now = time;
     print COMM_SOCKET $now;
}

   
    
    
    
  








Related examples in the same category

1.Using Socket to call a Perl CGI
2.Socket server
3.Perl Modules for Networking with Sockets
4.Open a socket
5.Post query to a CGI
6.Query a Perl CGI
7.Listen to a port
8.Using regular expresion to validate an IP address
9.Child handle and parent handle
10.Daytime client, using symbolic host and service names
11.Add a host, delete a host, add a user, delete a user, ping a host, list processes, list filesystems, lists hosts, and kill a process.
12.A Perl TCP server without the Socket module.
13.A Simple script to update your host/ip with dyndns.org service.