@@ -0,0 +1,83 | |||||
|
1 | /* | |||
|
2 | * ClienteUDP.c | |||
|
3 | * | |||
|
4 | * Fecha de creacion : Nov 2, 2009 | |||
|
5 | * Ultima modificacion : Nov 19, 2009 | |||
|
6 | * Autor : Jose Francisco Quenta C. | |||
|
7 | */ | |||
|
8 | ||||
|
9 | #include <stdio.h> | |||
|
10 | #include <stdlib.h> | |||
|
11 | #include <string.h> | |||
|
12 | #include <unistd.h> | |||
|
13 | #include <errno.h> | |||
|
14 | #include <sys/types.h> | |||
|
15 | #include <sys/socket.h> | |||
|
16 | #include <netinet/in.h> | |||
|
17 | #include <arpa/inet.h> | |||
|
18 | #include <netdb.h> | |||
|
19 | ||||
|
20 | #include "./Librerias/Mensajes.h" | |||
|
21 | ||||
|
22 | #define PUERTO_SERVIDOR 5500 | |||
|
23 | //#define DIR_SERVIDOR "10.10.12.255" | |||
|
24 | #define DIR_SERVIDOR "192.168.1.255" | |||
|
25 | ||||
|
26 | int ClienteUDP(char *opcion, char *valor){ | |||
|
27 | ||||
|
28 | int conexion_clienteFD; | |||
|
29 | struct sockaddr_in inf_servidor; | |||
|
30 | ||||
|
31 | int broadcast= 1; | |||
|
32 | int resultado; | |||
|
33 | int numbytes_enviados; | |||
|
34 | ||||
|
35 | char *buff_peticion; | |||
|
36 | char *comando= NULL; | |||
|
37 | ||||
|
38 | if (strcmp(opcion,"-l") == 0){ // Se reconoce el comando que se esta enviando | |||
|
39 | comando= "CARGA:"; | |||
|
40 | }else if(strcmp(opcion,"-c") == 0){ | |||
|
41 | comando= "CAMBIA:"; | |||
|
42 | }else if(strcmp(opcion,"-ch") == 0){ | |||
|
43 | comando= "CHEQUEO:"; | |||
|
44 | }else { | |||
|
45 | ERROR("OPCION INCORRECTA: {-l|-c|-ch}"); | |||
|
46 | return -1; | |||
|
47 | } | |||
|
48 | ||||
|
49 | buff_peticion= (char *) malloc(strlen(comando)+1+strlen(valor)+1); // Se arma el buffer a ser enviado. | |||
|
50 | strcpy(buff_peticion,comando); | |||
|
51 | strcat(buff_peticion,valor); | |||
|
52 | ||||
|
53 | /* Se establece el socket UDP */ | |||
|
54 | conexion_clienteFD= socket(AF_INET,SOCK_DGRAM,0); | |||
|
55 | if (conexion_clienteFD == -1){ | |||
|
56 | ERROR("No se pudo establecer el socket: socket()"); | |||
|
57 | return -1; | |||
|
58 | } | |||
|
59 | ||||
|
60 | /* Se establece el Broadcast con la funcion setsockpt() */ | |||
|
61 | resultado= setsockopt(conexion_clienteFD, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)); | |||
|
62 | if (resultado == -1){ | |||
|
63 | ERROR("No se pudo establecer la opcion de Broadcast: setsockopt()"); | |||
|
64 | return -1; | |||
|
65 | } | |||
|
66 | ||||
|
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 | /* 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)); | |||
|
75 | if(numbytes_enviados == -1){ | |||
|
76 | ERROR("Error de envio de datos: sendto()"); | |||
|
77 | return -1; | |||
|
78 | } | |||
|
79 | ||||
|
80 | close(conexion_clienteFD); | |||
|
81 | ||||
|
82 | return 0; | |||
|
83 | } |
General Comments 0
You need to be logged in to leave comments.
Login now