Advertisement
ganryu

server

Oct 14th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <sys/wait.h>
  11. #include <signal.h>
  12. #include <time.h>
  13.  
  14. #define MYPORT 3490 // Puerto al que conectarán los usuarios
  15. #define BACKLOG 100 // Cuántas conexiones pendientes se mantienen en cola
  16. #define MAXDATASIZE 100
  17.  
  18. void sigchld_handler(int s)
  19. {
  20.     while (wait(NULL) > 0)
  21.         ;
  22. }
  23.  
  24. typedef struct
  25. {
  26.     float temperatura;
  27.     char humedad;
  28.     unsigned int timestamp;
  29. } medicion;
  30.  
  31. void child(int *new_fd, char NOMBRE[], FILE *archivo)
  32. {
  33.     int receivedSize;
  34.     char caracterControl = 0x5A;
  35.     long unsigned int timestampServer;
  36.     int numbytes;
  37.     medicion myMedicion;
  38.  
  39.     printf("Por recibir... \n");
  40.  
  41.     if ((receivedSize = recv(*new_fd, NOMBRE, MAXDATASIZE - 1, 0)) == -1)
  42.     {
  43.         perror("receive");
  44.         exit(1);
  45.     }
  46.  
  47.     printf("¡Ya recibi!, recibi %d bytes \n", receivedSize);
  48.     NOMBRE[receivedSize] = '\0';
  49.  
  50.     printf("recibi: %s \n", NOMBRE);
  51.     printf("por enviar 0x5A...");
  52.  
  53.     if (send(*new_fd, &caracterControl, sizeof(char), 0) == -1)
  54.     {
  55.         perror("send");
  56.         exit(1);
  57.     }
  58.  
  59.     printf("Enviado! \n");
  60.  
  61.     while (1)
  62.     {
  63.         timestampServer = time(NULL);
  64.  
  65.         if ((time(NULL) - timestampServer) >= 1)
  66.         {
  67.             timestampServer = time(NULL);
  68.  
  69.             printf("Por recibir...");
  70.             if (numbytes = recv(*new_fd, &myMedicion, sizeof(medicion), 0) == -1)
  71.             {
  72.                 perror("Receive");
  73.                 exit(1);
  74.             }
  75.  
  76.             printf("Recibido!, cantidad %d \n", numbytes);
  77.             printf(
  78.                 "temperatura: %f \t, humedad: %d \t, timeStamp: %d \t \n",
  79.                 myMedicion.temperatura,
  80.                 myMedicion.humedad,
  81.                 myMedicion.timestamp);
  82.             fprintf(archivo, "temp");
  83.             fflush(archivo);
  84.             sleep(1);
  85.         }
  86.     }
  87. }
  88.  
  89. int main(void)
  90. {
  91.     int sockfd, new_fd;            // Escuchar sobre sock_fd, nuevas conexiones sobre new_fd
  92.     struct sockaddr_in my_addr;    // información sobre mi dirección
  93.     struct sockaddr_in their_addr; // información sobre la dirección del cliente
  94.     int sin_size;
  95.     struct sigaction sa;
  96.     int yes = 1;
  97.     char NOMBRE[MAXDATASIZE];
  98.     long unsigned int timestampServer;
  99.     FILE *archivo;
  100.     char array[256];
  101.  
  102.     // Sock stream = TCP
  103.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  104.     {
  105.         perror("socket");
  106.         exit(1);
  107.     }
  108.  
  109.     if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  110.     {
  111.         perror("setsockopt");
  112.         exit(1);
  113.     }
  114.  
  115.     my_addr.sin_family = AF_INET;         // Ordenación de bytes de la máquina
  116.     my_addr.sin_port = htons(MYPORT);     // short, Ordenación de bytes de la red
  117.     my_addr.sin_addr.s_addr = INADDR_ANY; // Rellenar con mi dirección IP
  118.  
  119.     memset(&(my_addr.sin_zero), '\0', 8); // Poner a cero el resto de la estructura
  120.  
  121.     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  122.     {
  123.         perror("bind");
  124.         exit(1);
  125.     }
  126.  
  127.     if (listen(sockfd, BACKLOG) == -1)
  128.     {
  129.         perror("listen");
  130.         exit(1);
  131.     }
  132.  
  133.     sa.sa_handler = sigchld_handler; // Eliminar procesos muertos
  134.     sigemptyset(&sa.sa_mask);
  135.     sa.sa_flags = SA_RESTART;
  136.  
  137.     if (sigaction(SIGCHLD, &sa, NULL) == -1)
  138.     {
  139.         perror("sigaction");
  140.         exit(1);
  141.     }
  142.  
  143.     archivo = fopen("sensores.txt", "w+");
  144.  
  145.     while (1)
  146.     { // main accept() loop
  147.         sin_size = sizeof(struct sockaddr_in);
  148.         if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
  149.         {
  150.             perror("accept");
  151.             continue;
  152.         }
  153.  
  154.         printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
  155.  
  156.         if (!fork())
  157.         { // Este es el proceso hijo
  158.  
  159.             close(sockfd); // El hijo no necesita este descriptor
  160.             child(&new_fd, NOMBRE, archivo);
  161.         }
  162.         close(new_fd); // El proceso padre no lo necesita
  163.         fclose(archivo);
  164.         exit(0);
  165.     }
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement