#include "nethelp.h"
#define MAX 100

/* invoke server using "xmyserver 3333" */

void * HandleClient(void *);

int main(int argc, char * argv[])
{
   int port, listenfd, serverfd;
   struct sockaddr clientaddr;
   int clientlen = sizeof(clientaddr);
   pthread_t tid;

   if(argc < 2)
   {  
      printf("Please supply the listening port number\n");
      exit(1);
   }

   port = atoi(argv[1]);
   listenfd = open_listenfd(port);
   
   while(1)
   {
      serverfd = accept(listenfd, &clientaddr, &clientlen);
      if(serverfd > 0)
         pthread_create(&tid, NULL, HandleClient, ((void*)serverfd));
   }
}

void * HandleClient(void * fd)
{
   char buf[MAX];
   int nb;

   int serverfd = (int)fd;
   nb = read(serverfd, buf, MAX);
   write(1, buf,nb);
   close(serverfd);
   return NULL;
}