ClienteUDP.c
83 lines
| 2.2 KiB
| text/x-c
|
CLexer
r14 | /* | |||
* 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" | ||||
int ClienteUDP(char *opcion, char *valor){ | ||||
int conexion_clienteFD; | ||||
struct sockaddr_in inf_servidor; | ||||
int broadcast= 1; | ||||
int resultado; | ||||
int numbytes_enviados; | ||||
char *buff_peticion; | ||||
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; | ||||
} | ||||
/* Se configura la estructura que contiene la informacion sobre el servidor: inf_servidor */ | ||||
inf_servidor.sin_family= AF_INET; | ||||
inf_servidor.sin_port= htons(PUERTO_SERVIDOR); | ||||
inf_servidor.sin_addr.s_addr= inet_addr(DIR_SERVIDOR); | ||||
memset(inf_servidor.sin_zero, '\0', sizeof(inf_servidor.sin_zero)); | ||||
/* Se procede a enviar el buffer */ | ||||
numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor)); | ||||
if(numbytes_enviados == -1){ | ||||
ERROR("Error de envio de datos: sendto()"); | ||||
return -1; | ||||
} | ||||
close(conexion_clienteFD); | ||||
return 0; | ||||
} | ||||