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