#include <time.h>                       /* rudimentary time support */
#include <stdlib.h>                     /* atoi support */
#include <signal.h>                     /* signal support */
#include <stdio.h>                      /* standard C io support */
#include <errno.h>                      /* standard C error stuff */
#include <ctype.h>                      /* supports the isblah stuff */
#include <string.h>                     /* useful string functions */
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <sys/ioctl.h>

#define BUFSIZE 1024

main ( argc, argv )
    int argc;
    char **argv;
{
    fd_set readfds;
    struct sockaddr_in addr;
    struct hostent *hp;
    int    udpSock;
    struct sockaddr_in from;
    int    fromlen, res;
    int    i;
    char   buf[BUFSIZE];
    char   *server=argv[1];
    char   *port=argv[2];
    char   *message=argv[3];
  
    if ( server == NULL )
    {
        fprintf(stderr, "Usage: uc server port message\n");
        exit(2);
    }

    if ((udpSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));

    if ((addr.sin_addr.s_addr = inet_addr(server)) == -1)
    {
        if ((hp = gethostbyname(server)) == NULL)
        {
            printf("Who is %s?\n", server);  
            exit(0);
        }
        else
        {
            addr.sin_addr.s_addr = *(long *) hp->h_addr;
        }
    }

    if (sendto(udpSock, message, strlen(message)+1, 0, &addr, sizeof(addr)) < 0)
    {
        perror("sendto");
        exit(11);
    }
}
