Solved

Unix Network Programming

Posted on 1999-01-23
5
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Even if you have implemented a Mobile Device Management solution company wide, it is a good idea to make sure you are taking into account all of the major risks to your electronic protected health information (ePHI).
PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

726 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