Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Network Programming

Posted on 1999-01-23
5
Medium Priority
?
296 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 200 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

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

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

For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

604 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