Solved

Unix Network Programming

Posted on 1999-01-23
5
278 Views
Last Modified: 2013-12-23
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!
0
Comment
Question by:boosta
5 Comments
 
LVL 2

Expert Comment

by:bertvermeerbergen
ID: 1583276
Do you have a specific server in mind, or is writing one the second part of your homework ?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1583277
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 );
0
 
LVL 4

Expert Comment

by:davidmwilliams
ID: 1583278
 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 ....
0
 

Author Comment

by:boosta
ID: 1583279
I want to get the time and date from a remote host!
0
 
LVL 4

Accepted Solution

by:
davidmwilliams earned 100 total points
ID: 1583280
 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";
       break;

      case 3:
        service = argv [2];

      case 2:
        host = argv [1];
        break

      default:
        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;
     }
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question