Solved

UNIX NETWORKING PROGRAMMING

Posted on 1998-11-13
4
219 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
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
davidmwilliams earned 200 total points
Comment Utility
#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
Comment Utility
Thanx a lot. Its appropriate and accurate.
0
 

Author Comment

by:boosta
Comment Utility
Thanx a lot. Its appropriate and accurate.
0
 
LVL 4

Expert Comment

by:davidmwilliams
Comment Utility
Glad to be able to help !
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I was recently sitting at a desk at work with one of my colleagues and needed some information on my home computer. He watched as I turned on my home computer, established a remote session into it, got the information I needed and then shut it down …
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

10 Experts available now in Live!

Get 1:1 Help Now