Unix Network Programming

I would like to request a server to give me its date and time using the read() and write() functions in C. I donot want to use the available functions of obtaining time from a remote host. I want the functions for obtaining the time and daytime coded for! Extra 10 points if answered by Mon 25 Jan 1999!
Who is Participating?
davidmwilliamsConnect With a Mentor Commented:
 I think we've already been through this :) but here's a C program that will read the date and time using the daytime service from a remote host.  Just pass the hostname (or "localhost") as a command-line parameter.

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

extern int errno;

int TCPdaytime (const char* host, const char *service);
int errexit (const char *format, ...);
int connectTCP (const char *host, const char *service)

#define LINELEN 128

int main (int argc, char *argv [])
    char *host = "localhost";
    char *service = "daytime";

    switch (argc)
      case 1:
       host = "localhost";

      case 3:
        service = argv [2];

      case 2:
        host = argv [1];

        fprintf (stderr, "usage: TCPdaytime [host [port]]\n");
        exit (1);

    TCPdaytime (host, service);
    exit (0);

     TCPdaytime (const char *host, const char *service)
       char buf [LINELEN + 1];
       int s, n;

       s = connectTCP (host, service);

       while ( (n = read (s, buf, LINELEN)) > 0)
         buf [n] = '\0';
         (void) fputs (buf, stdout);

     int errexit (const char *format, ...)
       va_list args;

       va_start (args, format);
       vfprintf (stderr, format, args);
       va_end (args);
       exit (1);

     int connectTCP (const char *host, const char *service)
       struct hostent *phe;
       struct servent *pse;
       struct protoent *ppe;
       struct sockaddr_in sin;
       int s, type;

       memset (&sin, 0, sizeof (sin));
       sin.sin_family = AF_INET;

       if (pse = getservbyname (service, "tcp"))
         sin.sin_port = pse->s_port;
       else if ( (sin.sin_port = htons ((u_short) atoi (service))) == 0)
         errexit ("Can't get \"%s\" service entry.\n", service);

       if (phe = gethostbyname (host))
         memcpy (&sin.sin_addr, phe->h_addr, phe->h_length);
       else if ((sin.sin_addr.s_addr = inet_addr (host)) == INADDR_NONE)
         errexit ("Can't get \"%s\" host entry.\n", host);

       if ((ppe = getprotobyname ("tcp")) == 0)
         errexit ("Can't get \"tcp\" protocol entry.\n");

       type = SOCK_STREAM;

       s = socket (PF_INET, type, ppe->p_proto);
       if (s < 0)
         errexit ("Can't create socket: %s\n", strerror (errno));

       if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
         errexit ("Can't connect to %s.%s: %s\n", host, service, strerror (errno));

       return s;
Do you have a specific server in mind, or is writing one the second part of your homework ?
not the way you usually do it, but simple:

      file *fid =popen( "telnet remote_host 13 2>&1|awk 'NR==4 {print}', "r" );
      fscanf( fid, "%s%", time_str );
 Can you rephrase the question?
  You want to get the date and time from a server - but not from a remote host.  So, in other words, when you say 'server', you mean the local system?
  There are simple C calls that will return the local date and time - you don't need to use read/write unless you are trying to access a file or pipe or socket - and I can't see why you would want any of these, except a socket - and even then, only if you were going to access a remote host ....
boostaAuthor Commented:
I want to get the time and date from a remote host!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.