#!/usr/bin/perl # # ICS 243d - Internet Technology # Spring Quarter 2001 # Roberto Silveira Silva Filho ID# 85849631 # Modified Daytime Client # Assignment 2 # # based on file: tcp_echo_serv1.pl # form the Book: Network Programming with Perl by Lincoln D. Stein # ---------------------------------------------------------- use strict; use Socket; use Time::Local; use constant DEFAULT_ADDR => '127.0.0.1'; use constant PORT => 13; use constant IPPROTO_TCP => 6; my %month_hash = ( "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12); # Return the difference, in seconds, between date2 and date1. sub computeDifference { my $date1 = shift; my $date2 = shift; # Convert the string, separated by space, in an array my @dateArray1 = split(" ", $date1); my @dateArray2 = split(" ", $date2); # Get the hour, minute, second from the time in the date array my @timeArray1 = split(":", $dateArray1[3]); my @timeArray2 = split(":", $dateArray2[3]); my $difference = 0; my $time1 = timelocal($timeArray1[2], $timeArray1[1], $timeArray1[0], $dateArray1[2], $month_hash{"$dateArray1[1]"}, $dateArray1[5]); my $time2 = timelocal($timeArray2[2], $timeArray2[1], $timeArray2[0], $dateArray2[2], $month_hash{"$dateArray2[1]"}, $dateArray2[5]); return $time2 - $time1; } my $port = 16004; my @address = ("rodan.ics.uci.edu", "igor.ics.uci.edu"); my @date; # temporary dates to be read from the servers in the array above my @time; # stores the corresponding epoch seconds of the date array. my $difference; # the difference between the two dates my $addressLength = @address; for (my $i=0; $i<$addressLength; $i++) { #print " iteraction $i \n"; my $currentaddress = $address[$i]; my $packed_addr = inet_aton($currentaddress); my $destination = sockaddr_in(PORT,$packed_addr); socket(SOCK,PF_INET,SOCK_STREAM,IPPROTO_TCP) or die "Can't make socket: $!"; connect(SOCK, $destination) or die "Can't connect to $destination: $!"; # Read the dates from the sockets opened and print to the standard error. @date[$i] = ; warn "Date read from $currentaddress is $date[$i]"; close SOCK; } # Prints the home page whith the resoults computed in this example. print "Connection: close\n"; print "Content-type: text/html\n\n"; print ""; print "Modified Daytime Client"; print "\n"; print "

Modified Daytime Client


\n"; for (my $i=0; $i<$addressLength; $i++) { print "
Date read from $address[$i] is $date[$i]
\n"; } $difference = computeDifference($date[0], $date[1]); print "

The time difference, between the two servers, is $difference seconds
\n"; print "


\n\n";