
/*SendCMD_ACK_client.c
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>

void timeout(int s)
{
	printf("TIMEOUT\n");
	exit(1);
} 


int main() {

	  int		    nodeID;
	  int                fd;
	  int                err;
	  struct sockaddr_in saddr;
	  int               command;
	  char 		    buff[10];
		

	signal(SIGALRM, timeout);

	if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		printf("Errore nella creazione della socket\n");

	  saddr.sin_family      = AF_INET;
	  saddr.sin_addr.s_addr = inet_addr("192.168.1.101");		//indirizzo IP in rete locale
	  saddr.sin_port        = htons(1745);


	  err = connect( fd, (struct sockaddr *) &saddr, sizeof( saddr ) );

	  if( err < 0 ) {
	    printf( "Connection error.\n" );
	    exit( 1 );
	  }



	printf("Select the node ID:\n");

	scanf("%d", &nodeID);

	printf("Select the command to execute:\n");
	printf("%d = Turn on the LED\n",1);
	printf("%d = Turn off the LED\n",0);


	scanf("%d", &command);

	
	send( fd, &nodeID, 1, 0 );	 

	send( fd, &command, 1, 0 );


	  printf( "Richiesta fatta al server\n");


	alarm(5);

	  recv( fd, buff, sizeof(buff), 0 );

	  printf("Risposta del server: %s\n",buff);

	alarm(0);

	  close( fd );
	  
	  return 0;
  
}





