diff --git a/arst.c b/arst.c new file mode 100644 index 0000000000000000000000000000000000000000..18636c22b7d7a6f61c9da4071ffabca67992c237 --- /dev/null +++ b/arst.c @@ -0,0 +1,126 @@ +/** +* Practica Tema 6: DAYTIME TCP +* +* Mata, David +* +* +*/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/ip.h> // Define sockaddr_in +#include <errno.h> // Para usar errno y perror() +#include <unistd.h> + +int main(int argc, char *argv[]) +{ + + struct servent *servicio; // Estructura para guardar el puerto + struct sockaddr_in direcccion; // Estructura para guardar la ip del servidor + + int puerto; + int numero_socket; + char mensaje[30] = "¿Que dia es hoy?"; + char respuesta[100]; // Buffer para almacenar la respuesta del servidor + struct sockaddr_in myaddr; // Estructura para guardar la direccion + myaddr.sin_family = AF_INET; // Sockets IPv4 + myaddr.sin_port = 0; // Si cliente, no indicar puerto. + myaddr.sin_addr.s_addr = INADDR_ANY; + + // Comprobamos que el numero de argumentos sea correcto + if (argc < 2 || argc > 5) + { + fprintf(stderr, "Error en el numero de argumentos"); + exit(EXIT_FAILURE); + } + // Transforma la direccion IP en numero entero de 32bits + if (inet_aton(argv[1], &direcccion.sin_addr) == 0) + { + fprintf(stderr, "Error al transfromar la direccion IP"); + exit(EXIT_FAILURE); + } + // Comprueba si se pasa el numero de puerto con la opcion -p + if (argc == 4 && strcmp(argv[2], "-p") == 0) + { + puerto = htons(atoi(argv[3])); // Convierte el puerto de una cadena a un entero + } + else + { + // Si no introduce puerto lo obtiene del servicio daytime(13 UDP) + servicio = getservbyname("daytime", "tcp"); + // Comprobamos que no hay ningun error + if (servicio == NULL) + { + fprintf(stderr, "No se consigue encontrar el servicio"); + exit(EXIT_FAILURE); + } + puerto = servicio->s_port; // Asociamos el puerto a la estructura del servidor + + } + + // Creamos la variable sockect + numero_socket = socket(AF_INET, SOCK_STREAM, 0); + // Comprobamos que no hay ningun error + if (numero_socket < 0) + { + perror("Error al crear el socket"); + exit(EXIT_FAILURE); + } + + + // Enlazamos el socket con nuestra direccion + int sock_bind = bind(numero_socket, (struct sockaddr *)&myaddr, sizeof(myaddr)); + // Comprobamos que no hay ningun error + if (sock_bind < 0) + { + perror("Error en la funcion bind"); + exit(EXIT_FAILURE); + } + direcccion.sin_family = AF_INET; // Asociamos la familia(Sockets IPv4) + direcccion.sin_port = puerto; // Devuelve el puerto en formato red + + // Establece la conexion con el servidor a traves de un sockect + int sockect_connect = connect(numero_socket, (struct sockaddr *)&direcccion, sizeof(direcccion)); + // Comprobamos que no hay ningun error + if (sockect_connect < 0) + { + perror("Error en la funcion connect"); + exit(EXIT_FAILURE); + } + + // Enviamos el mensaje al servidor a traves del sockect TCP + int sock_send = send(numero_socket, mensaje, sizeof(mensaje), 0); + // Comprobamos que no hay ningun error + if (sock_send < 0) + { + perror("Error en la funcion send"); + exit(EXIT_FAILURE); + } + + // Recibimos el mensaje del servidor a traves del sockect UDP + int sockect_recv = recv(numero_socket, respuesta, sizeof(respuesta), 0); + // Comprobamos que no hay ningun error + if (sockect_recv < 0) + { + perror("Error en la funcion recv"); + exit(EXIT_FAILURE); + } + + // Imprimimos el mensaje del servidor + printf("%s\n", respuesta); + + // Notifica de que se va a cerrar la conexion + shutdown(numero_socket, SHUT_RDWR); + + + + // Cerramos el sockect + close(numero_socket); + + exit(0); +}