|
|
/*
|
|
|
* ClienteUDP.c
|
|
|
*
|
|
|
* Fecha de creacion : Nov 2, 2009
|
|
|
* Ultima modificacion : Nov 19, 2009
|
|
|
* Autor : Jose Francisco Quenta C.
|
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <unistd.h>
|
|
|
#include <errno.h>
|
|
|
#include <sys/types.h>
|
|
|
#include <sys/socket.h>
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
#include <netdb.h>
|
|
|
|
|
|
#include "./Librerias/Mensajes.h"
|
|
|
|
|
|
#define PUERTO_SERVIDOR 5500
|
|
|
//#define DIR_SERVIDOR "10.10.12.255"
|
|
|
#define DIR_SERVIDOR "192.168.1.255"
|
|
|
#define TAM_BUFFER 100
|
|
|
|
|
|
|
|
|
int ClienteUDP(char *opcion, char *valor){
|
|
|
|
|
|
int conexion_clienteFD;
|
|
|
struct sockaddr_in inf_servidor, inf_servidor_2;
|
|
|
struct sockaddr_storage inf_cliente;
|
|
|
|
|
|
int broadcast= 1;
|
|
|
int resultado;
|
|
|
int numbytes_enviados;
|
|
|
int numbytes_recibidos;
|
|
|
size_t addr_len;
|
|
|
|
|
|
char *buff_peticion;
|
|
|
char *buff_rx = (char *) malloc(TAM_BUFFER);
|
|
|
char *comando= NULL;
|
|
|
|
|
|
if (strcmp(opcion,"-l") == 0){ // Se reconoce el comando que se esta enviando
|
|
|
comando= "CARGA:";
|
|
|
}else if(strcmp(opcion,"-c") == 0){
|
|
|
comando= "CAMBIA:";
|
|
|
}else if(strcmp(opcion,"-ch") == 0){
|
|
|
comando= "CHEQUEO:";
|
|
|
}else {
|
|
|
ERROR("OPCION INCORRECTA: {-l|-c|-ch}");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
buff_peticion= (char *) malloc(strlen(comando)+1+strlen(valor)+1); // Se arma el buffer a ser enviado.
|
|
|
strcpy(buff_peticion,comando);
|
|
|
strcat(buff_peticion,valor);
|
|
|
|
|
|
/* Se establece el socket UDP */
|
|
|
conexion_clienteFD= socket(AF_INET,SOCK_DGRAM,0);
|
|
|
if (conexion_clienteFD == -1){
|
|
|
ERROR("No se pudo establecer el socket: socket()");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* Se establece el Broadcast con la funcion setsockpt()
|
|
|
resultado= setsockopt(conexion_clienteFD, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
|
|
if (resultado == -1){
|
|
|
ERROR("No se pudo establecer la opcion de Broadcast: setsockopt()");
|
|
|
return -1;
|
|
|
}*/
|
|
|
|
|
|
/* Configuracion para el bind() */
|
|
|
inf_servidor.sin_family= AF_INET;
|
|
|
inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
|
|
|
//inf_servidor.sin_port= 0;
|
|
|
//inf_servidor.sin_addr.s_addr= inet_addr("192.168.1.255");
|
|
|
inf_servidor.sin_addr.s_addr= INADDR_ANY;
|
|
|
memset(inf_servidor.sin_zero, '\0', sizeof(inf_servidor.sin_zero));
|
|
|
|
|
|
/* Configuración para el sendto() */
|
|
|
inf_servidor_2.sin_family= AF_INET;
|
|
|
inf_servidor_2.sin_port= htons(PUERTO_SERVIDOR);
|
|
|
//inf_servidor.sin_port= 0;
|
|
|
inf_servidor_2.sin_addr.s_addr= inet_addr("192.168.1.13");
|
|
|
//inf_servidor_2.sin_addr.s_addr= INADDR_ANY;
|
|
|
memset(inf_servidor_2.sin_zero, '\0', sizeof(inf_servidor_2.sin_zero));
|
|
|
|
|
|
/* Se asocia el socket a un puerto y una IP */
|
|
|
resultado = bind(conexion_clienteFD,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
|
|
|
if (resultado== -1){
|
|
|
ERROR_FATAL("No se establecio correctamente el socket: bind()");
|
|
|
}
|
|
|
|
|
|
/* Se procede a enviar el buffer */
|
|
|
//numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
|
|
|
numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor_2,sizeof(inf_servidor_2));
|
|
|
if(numbytes_enviados == -1){
|
|
|
ERROR("Error de envio de datos: sendto()");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* Se espera respuesta del servidor */
|
|
|
addr_len = sizeof(inf_cliente);
|
|
|
printf("Esperando respuesta del servidor\n");
|
|
|
numbytes_recibidos = recvfrom(conexion_clienteFD, buff_rx, TAM_BUFFER-1, 0, (struct sockaddr *)&inf_cliente, &addr_len);
|
|
|
if (numbytes_recibidos == -1){
|
|
|
ERROR_FATAL("Error en la recepcion de datos: recvfrom()");
|
|
|
}
|
|
|
printf("Recibida la respuesta del servidor\n");
|
|
|
close(conexion_clienteFD);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|