#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "socktools.h"

extern int  PORT;
extern char *SERV;
char name[128];

void calcul( int sd, struct sockaddr *serv)
{ int  val, retry ;
  struct sockaddr slave; 
  while ( 1 ) {
      sendpaquetto( sd, serv, 0);
      retry = 2;
      do {
         val = nobloqpaquetfrom( sd, &slave);
	 if ( val < 0) {
	   printf("\nPas de reponse %d %s", retry, name); 
	   sleep(1);
	 }
         retry--;
      } while  (( val < 0 ) && retry ); 
      if (  val <= 0   )  return;
      printf("\njob = %d %s", val, name);
      val = val * val;  sleep(5);
      sendpaquetto( sd, &slave, val);
  }
}

int main(int argc, char *argv[])
{ int   sd;
  struct sockaddr_in serv;
  
  if ( ! argok( argc, argv) ) return 1;
  gethostname(name, 128);
  sd = socket( AF_INET, SOCK_DGRAM, 0);
  serv = sockaddrinbyname( SERV , PORT );
 
  calcul( sd , (struct sockaddr*) &serv);
  
  close(sd);
  return 0;
}
