#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define BUFLEN 512
#define NPACK 10
#define PORT 5667

void pexit(char *str)
{
   perror(str);
   exit(1);
}




#define SRV_IP "128.193.0.39"

int main(int argc, char **argv)
{
   struct sockaddr_in sin_other;
   int fd, i, slen = sizeof(sin_other);
   char buf[BUFLEN];

   if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
      pexit("socket");

   memset((char *) &sin_other, sizeof(sin_other), 0);
   sin_other.sin_family = AF_INET;
   sin_other.sin_port = htons(PORT);
   if (inet_aton(((argc > 1) ? argv[1] : SRV_IP), &sin_other.sin_addr) ==
       0) {
      fprintf(stderr, "inet_aton() failed\n");
      exit(1);
   }
   i=0;
   while ((fgets(buf, sizeof(buf), stdin)) != NULL) {
      printf("Sending packet %d\n", i);
      snprintf(buf, sizeof(buf), "This is packet %d\n", i++);
      if ((write(fd, buf, strlen(buf))) == (-1))
	 pexit("sendto()");
   }
   close(fd);
   return 0;
}
