Solved

Unix Network Programming

Posted on 1999-01-23
5
267 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now