🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.linux.misc
Article #572

Re: Setting linux time from a time server

#572
From: Grahame M. Kelly
Date: Thu, 09 Jan 1997 00:00
142 lines
4338 bytes
This is a multi-part message in MIME format.

--------------13369D6D36C5B85A62F48C73
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Sandy Knight wrote:
> 
> I would like tosynchronize my linux server to a timeserver that we have.  The
> problem that i have is that I have absolutely no idea where to begin with
> this.
>

Hi Sandy,

I enclose a program you can use to get the time-of-day from a external
internet server, which is one of the excerises in the book:
"Unix Systems Programming for SVR4 by David Curry" a O'Reilly & Assoc
book ISBN 1-56592-163-1  and WORTH every cent.

You is very easy to make a slight code change to set your machines
time to match that of the Server. (so course +/- a faction of a 
second or two dependent on the servers location and speed of connection)

If you wish to just get the time from a local LAN Server
or workstation then they also give examples. You will need to have the
/etc/inetd.conf line that reads :

#daytime  dgram  udp  wait  root  internal 

enabled by removing the # in the first character position.


See Chapter 14 "Networking with Sockets" page 407.
and Chapter 13 "Interprocess Communications" pages 372. etc.
for further and better informed details. I again highly recommend
that you purchase the book; you will want to develop more and more
things as time goes by.

I hope this helps,
Cheers Grahame

--------------13369D6D36C5B85A62F48C73
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="daytime.c"

/*
	Program	-	To get the Day Time form each server on command line
			across the network

	Author	-	Chapter 14: Page 407: Networking with Sockets
			"UNIX System Programming for SVR4 by David A. Curry"
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>

#define SERVICENAME "daytime"

int main (int argc, char **argv)
{
	int n, s, len;
	char buf[1024];
	char *hostname;
	struct hostent *hp;
	struct servent *sp;
	struct sockaddr_in name, from;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s hostname [hostname...]\n", *argv);
		exit(1);
	}

	/* Look up our service. We want the UDP version */
	if ((sp = getservbyname(SERVICENAME, "udp")) == NULL) {
		fprintf(stderr, "%s/udp: unknown service.\n", SERVICENAME);
		exit(1);
	}

	while (--argc) {
		hostname = *++argv;
	
		/* Look up the hosts network address */
		if ((hp = gethostbyname(hostname)) == NULL) {
			fprintf(stderr, "%s: unknown host.\n", hostname);
			continue;
		}

		/* Create a socket in the INET domain */
		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
			perror("daytime socket");
			exit(1);
		}

		memset(&name, 0, sizeof(struct sockaddr_in));
		name.sin_family = AF_INET;
		name.sin_port = sp->s_port;
		memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
		len = sizeof(struct sockaddr_in);

		/* Send a packet to the server */
		memset(buf, 0, sizeof(buf));

		n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *) &name,
			sizeof(struct sockaddr_in));

		if (n < 0) {
			perror("daytime sendto");
			exit(1);
		}

		/* Receive a packet back */
		len = sizeof(struct sockaddr_in);
		n = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &from,
			&len);

		if (n < 0) {
			perror("daytime received from");
			exit(1);
		}

		/* Print the packet */
		buf[n] = '\0';
		printf("%s: %s", hostname, buf);

		/* Close the socket */
		close(s);
	}

	exit(0);
}


		

--------------13369D6D36C5B85A62F48C73--
Technical Details
Message-ID: <32D4A05B.3049A943@zip.com.au>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <5b1dsn$cnc@hpcc48.corp.hp.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1103af,bfc01b3bf66d85a4
X-Google-Attributes: gid1103af,public
X-Google-Thread: 102983,d12885de0b424b56,start
X-Google-Attributes: gid102983,public
From: "Grahame M. Kelly" <gmkelly@zip.com.au>
Subject: Re: Setting linux time from a time server
Date: 1997/01/09
Message-ID: <32D4A05B.3049A943@zip.com.au>
X-Deja-AN: 208642267
references: <5b1dsn$cnc@hpcc48.corp.hp.com>
content-type: multipart/mixed; boundary="------------13369D6D36C5B85A62F48C73"
organization: The Zipsters
mime-version: 1.0
newsgroups: comp.os.linux.misc,aus.computers.linux
x-mailer: Mozilla 2.02 (X11; I; Linux 2.0.23 i586)