?
Solved

UNIX NETWORKING PROGRAMMING

Posted on 1998-11-13
4
Medium Priority
?
233 Views
Last Modified: 2013-12-23
I want to write a program in C (under unix) in which a user specifies a remote host name (or ip address) and request a service such as the current time of the remote server. The program should convert the username to an ip address(if username is given), locate the remote host, request the service required by user and display the output(in a user friendly manner) on the user's terminal. Please help!!
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
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
davidmwilliams earned 800 total points
ID: 1582608
#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
 

Author Comment

by:boosta
ID: 1582609
Thanx a lot. Its appropriate and accurate.
0
 

Author Comment

by:boosta
ID: 1582610
Thanx a lot. Its appropriate and accurate.
0
 
LVL 4

Expert Comment

by:davidmwilliams
ID: 1582611
Glad to be able to help !
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

752 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