##// END OF EJS Templates
Cambios en el cliente del Control_Module para pruebas de respuesta hacia Central_Control_Module
imanay -
r33:34
parent child
Show More
@@ -1,83 +1,114
1 1 /*
2 2 * ClienteUDP.c
3 3 *
4 4 * Fecha de creacion : Nov 2, 2009
5 5 * Ultima modificacion : Nov 19, 2009
6 6 * Autor : Jose Francisco Quenta C.
7 7 */
8 8
9 9 #include <stdio.h>
10 10 #include <stdlib.h>
11 11 #include <string.h>
12 12 #include <unistd.h>
13 13 #include <errno.h>
14 14 #include <sys/types.h>
15 15 #include <sys/socket.h>
16 16 #include <netinet/in.h>
17 17 #include <arpa/inet.h>
18 18 #include <netdb.h>
19 19
20 20 #include "./Librerias/Mensajes.h"
21 21
22 22 #define PUERTO_SERVIDOR 5500
23 23 //#define DIR_SERVIDOR "10.10.12.255"
24 24 #define DIR_SERVIDOR "192.168.1.255"
25 #define TAM_BUFFER 100
26
25 27
26 28 int ClienteUDP(char *opcion, char *valor){
27 29
28 30 int conexion_clienteFD;
29 struct sockaddr_in inf_servidor;
31 struct sockaddr_in inf_servidor, inf_servidor_2;
32 struct sockaddr_storage inf_cliente;
30 33
31 34 int broadcast= 1;
32 35 int resultado;
33 36 int numbytes_enviados;
37 int numbytes_recibidos;
38 size_t addr_len;
34 39
35 40 char *buff_peticion;
41 char *buff_rx = (char *) malloc(TAM_BUFFER);
36 42 char *comando= NULL;
37 43
38 44 if (strcmp(opcion,"-l") == 0){ // Se reconoce el comando que se esta enviando
39 45 comando= "CARGA:";
40 46 }else if(strcmp(opcion,"-c") == 0){
41 47 comando= "CAMBIA:";
42 48 }else if(strcmp(opcion,"-ch") == 0){
43 49 comando= "CHEQUEO:";
44 50 }else {
45 51 ERROR("OPCION INCORRECTA: {-l|-c|-ch}");
46 52 return -1;
47 53 }
48 54
49 55 buff_peticion= (char *) malloc(strlen(comando)+1+strlen(valor)+1); // Se arma el buffer a ser enviado.
50 56 strcpy(buff_peticion,comando);
51 57 strcat(buff_peticion,valor);
52 58
53 59 /* Se establece el socket UDP */
54 60 conexion_clienteFD= socket(AF_INET,SOCK_DGRAM,0);
55 61 if (conexion_clienteFD == -1){
56 62 ERROR("No se pudo establecer el socket: socket()");
57 63 return -1;
58 64 }
59 65
60 /* Se establece el Broadcast con la funcion setsockpt() */
66 /* Se establece el Broadcast con la funcion setsockpt()
61 67 resultado= setsockopt(conexion_clienteFD, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
62 68 if (resultado == -1){
63 69 ERROR("No se pudo establecer la opcion de Broadcast: setsockopt()");
64 70 return -1;
71 }*/
72
73 /* Configuracion para el bind() */
74 inf_servidor.sin_family= AF_INET;
75 inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
76 //inf_servidor.sin_port= 0;
77 //inf_servidor.sin_addr.s_addr= inet_addr("192.168.1.255");
78 inf_servidor.sin_addr.s_addr= INADDR_ANY;
79 memset(inf_servidor.sin_zero, '\0', sizeof(inf_servidor.sin_zero));
80
81 /* Configuración para el sendto() */
82 inf_servidor_2.sin_family= AF_INET;
83 inf_servidor_2.sin_port= htons(PUERTO_SERVIDOR);
84 //inf_servidor.sin_port= 0;
85 inf_servidor_2.sin_addr.s_addr= inet_addr("192.168.1.13");
86 //inf_servidor_2.sin_addr.s_addr= INADDR_ANY;
87 memset(inf_servidor_2.sin_zero, '\0', sizeof(inf_servidor_2.sin_zero));
88
89 /* Se asocia el socket a un puerto y una IP */
90 resultado = bind(conexion_clienteFD,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
91 if (resultado== -1){
92 ERROR_FATAL("No se establecio correctamente el socket: bind()");
65 93 }
66 94
67 /* Se configura la estructura que contiene la informacion sobre el servidor: inf_servidor */
68 inf_servidor.sin_family= AF_INET;
69 inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
70 inf_servidor.sin_addr.s_addr= inet_addr(DIR_SERVIDOR);
71 memset(inf_servidor.sin_zero, '\0', sizeof(inf_servidor.sin_zero));
72
73 95 /* Se procede a enviar el buffer */
74 numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
96 //numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
97 numbytes_enviados= sendto(conexion_clienteFD,buff_peticion,strlen(buff_peticion),0,(struct sockaddr *)&inf_servidor_2,sizeof(inf_servidor_2));
75 98 if(numbytes_enviados == -1){
76 99 ERROR("Error de envio de datos: sendto()");
77 100 return -1;
78 101 }
79 102
103 /* Se espera respuesta del servidor */
104 addr_len = sizeof(inf_cliente);
105 printf("Esperando respuesta del servidor\n");
106 numbytes_recibidos = recvfrom(conexion_clienteFD, buff_rx, TAM_BUFFER-1, 0, (struct sockaddr *)&inf_cliente, &addr_len);
107 if (numbytes_recibidos == -1){
108 ERROR_FATAL("Error en la recepcion de datos: recvfrom()");
109 }
110 printf("Recibida la respuesta del servidor\n");
80 111 close(conexion_clienteFD);
81 112
82 113 return 0;
83 114 }
General Comments 0
You need to be logged in to leave comments. Login now