#include <krb.h>
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>       /* obligatory includes */
#include <signal.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>

#define MAXHOSTNAME REALM_SZ

main()
{ 
    int s, t;
    fd_set read_fd;
    
    if ((s= establish()) < 0) {  /* plug in the phone */
	perror("establish");
	exit(1);
    }
    
    while(1) {                          /* loop for phone calls */
	FD_ZERO(&read_fd);
	FD_SET(s, &read_fd);
	select(s+1, &read_fd, (fd_set *)0, (fd_set *)0, NULL);

	if ((t= get_connection(s)) < 0) { /* get a connection */
	    if (errno == EINTR)             /* EINTR might happen on accept(), */
		continue;                     /* try again */
	    perror("accept");               /* bad */
	    exit(1);
	}
	do_something(t);
	close(t);
	listen(s, 1);
    }
}



int establish()
{ 
    char   myname[MAXHOSTNAME+1];
    int    s;
    u_short portnum = 2048;
    struct sockaddr_in sa;
    struct hostent *hp;
    struct servent *sp;


    bzero(&sa,sizeof(struct sockaddr_in));      /* clear our address */
    gethostname(myname,MAXHOSTNAME);            /* who are we? */
    hp= gethostbyname(myname);                  /* get our address info */
    if (hp == NULL)                             /* we don't exist !? */
	return(-1);
    sa.sin_family= hp->h_addrtype;              /* this is our host address */
    sa.sin_port= htons(portnum);                /* this is our port number */
    if ((s= socket(AF_INET,SOCK_STREAM,0)) < 0) /* create socket */
	return(-1);
    if (bind(s,&sa,sizeof sa,0) < 0) {
	close(s);
	return(-1);                               /* bind address to socket */
    }
    listen(s, 1);                               /* max # of queued connects */
    return(s);
}


int get_connection(s)
int s;                    /* socket created with establish() */
{ 
    struct sockaddr_in isa; /* address of socket */
    int i;                  /* size of address */
    int t;                  /* socket of connection */
    
    i = sizeof(isa);                   /* find socket's address */
    getsockname(s,&isa,&i);            /* for accept() */
    
    if ((t = accept(s,&isa,&i)) < 0)   /* accept connection if there is one */
	return(-1);
    return(t);
}

do_something(t)
int t;
{
  char c;

  printf("Got connection. . .\nReceived: ");

  fflush(stdout);

  while (read (t, &c, 1) > 0) {
    printf("%x ", c);
    fflush(stdout);
  }

  return;
}

