programming-examples/perl/Network/Time Client.pl
2019-11-15 12:59:38 +01:00

30 lines
817 B
Perl

#!/usr/local/bin/perl
#Usage: timeclient [server_host_name]
$them = 'localhost';
$port = 9876 ;
$AF_INET = 2;
$SOCK_STREAM = 1;
$sockaddr = 'S n a4 x8';
($name, $aliases, $proto) = getprotobyname('tcp');
($name,$aliases, $port, $proto)=getservbyname($port, 'tcp') unless $port =~ /^\d+$/;
($name,$aliases, $type, $len, $thataddr)=gethostbyname($them);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);
if ( socket(SOCK, $AF_INET, $SOCK_STREAM, $proto ) ){
print "Socket ok.\n";
}
else {
die $!;
}
if(connect(SOCK, $that)){
print "Connect ok.\n";
}
else {
die $!;
}
select(SOCK); $| = 1; select (STDOUT);
$hertime = <SOCK>;
close(SOCK);
print "Server machine time is: $hertime\n";
@now = localtime($hertime);
print "\t$now[2]:$now[1] ", $now[4]+1,"/$now[3]/$now[5]\n";