Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <time.h>
- #define MYPORT 3490 // Puerto al que conectarán los usuarios
- #define BACKLOG 100 // Cuántas conexiones pendientes se mantienen en cola
- #define MAXDATASIZE 100
- void sigchld_handler(int s)
- {
- while (wait(NULL) > 0)
- ;
- }
- typedef struct
- {
- float temperatura;
- char humedad;
- unsigned int timestamp;
- } medicion;
- void child(int *new_fd, char NOMBRE[], FILE *archivo)
- {
- int receivedSize;
- char caracterControl = 0x5A;
- long unsigned int timestampServer;
- int numbytes;
- medicion myMedicion;
- printf("Por recibir... \n");
- if ((receivedSize = recv(*new_fd, NOMBRE, MAXDATASIZE - 1, 0)) == -1)
- {
- perror("receive");
- exit(1);
- }
- printf("¡Ya recibi!, recibi %d bytes \n", receivedSize);
- NOMBRE[receivedSize] = '\0';
- printf("recibi: %s \n", NOMBRE);
- printf("por enviar 0x5A...");
- if (send(*new_fd, &caracterControl, sizeof(char), 0) == -1)
- {
- perror("send");
- exit(1);
- }
- printf("Enviado! \n");
- while (1)
- {
- timestampServer = time(NULL);
- if ((time(NULL) - timestampServer) >= 1)
- {
- timestampServer = time(NULL);
- printf("Por recibir...");
- if (numbytes = recv(*new_fd, &myMedicion, sizeof(medicion), 0) == -1)
- {
- perror("Receive");
- exit(1);
- }
- printf("Recibido!, cantidad %d \n", numbytes);
- printf(
- "temperatura: %f \t, humedad: %d \t, timeStamp: %d \t \n",
- myMedicion.temperatura,
- myMedicion.humedad,
- myMedicion.timestamp);
- fprintf(archivo, "temp");
- fflush(archivo);
- sleep(1);
- }
- }
- }
- int main(void)
- {
- int sockfd, new_fd; // Escuchar sobre sock_fd, nuevas conexiones sobre new_fd
- struct sockaddr_in my_addr; // información sobre mi dirección
- struct sockaddr_in their_addr; // información sobre la dirección del cliente
- int sin_size;
- struct sigaction sa;
- int yes = 1;
- char NOMBRE[MAXDATASIZE];
- long unsigned int timestampServer;
- FILE *archivo;
- char array[256];
- // Sock stream = TCP
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- {
- perror("socket");
- exit(1);
- }
- if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
- {
- perror("setsockopt");
- exit(1);
- }
- my_addr.sin_family = AF_INET; // Ordenación de bytes de la máquina
- my_addr.sin_port = htons(MYPORT); // short, Ordenación de bytes de la red
- my_addr.sin_addr.s_addr = INADDR_ANY; // Rellenar con mi dirección IP
- memset(&(my_addr.sin_zero), '\0', 8); // Poner a cero el resto de la estructura
- if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
- {
- perror("bind");
- exit(1);
- }
- if (listen(sockfd, BACKLOG) == -1)
- {
- perror("listen");
- exit(1);
- }
- sa.sa_handler = sigchld_handler; // Eliminar procesos muertos
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_RESTART;
- if (sigaction(SIGCHLD, &sa, NULL) == -1)
- {
- perror("sigaction");
- exit(1);
- }
- archivo = fopen("sensores.txt", "w+");
- while (1)
- { // main accept() loop
- sin_size = sizeof(struct sockaddr_in);
- if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
- {
- perror("accept");
- continue;
- }
- printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
- if (!fork())
- { // Este es el proceso hijo
- close(sockfd); // El hijo no necesita este descriptor
- child(&new_fd, NOMBRE, archivo);
- }
- close(new_fd); // El proceso padre no lo necesita
- fclose(archivo);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement