#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "uici.h"
int copyfile(int fromfd, int tofd);

int main(int argc, char *argv[])
{
   int bytes_copied;
   char client[MAX_CANON];
   int communfd;
   int listenfd;
   u_port_t portnumber;
 
   if (argc != 2) {
      fprintf(stderr, "Usage: %s port\n", argv[0]);
      return 1;   
   }  
   portnumber = (u_port_t) atoi(argv[1]);
   if ((listenfd = u_open(portnumber)) < 0) {
      fprintf(stderr,"Listen endpoint creation failed: %s\n",
              u_strerror(listenfd));
      return 1;
   }      
   fprintf(stderr, "[%ld]: Waiting for the first connection on port %d\n",
                    (long)getpid(), (int)portnumber);
   for ( ; ; ) {
      if ((communfd = u_accept(listenfd, client, MAX_CANON)) != -1) {
         fprintf(stderr, "A connection has been received from %s\n",client);
         bytes_copied = copyfile(communfd, STDOUT_FILENO);
         close(communfd);
         fprintf(stderr, "[%ld]:Bytes received = %d\n",
                          (long) getpid(), bytes_copied);
      }
      else
         perror("Accept failed");
   }
}

