daily pastebin goal
55%
SHARE
TWEET

server

a guest Nov 21st, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * @file server.c
  3. * @brief Description
  4. * @date 2017-10-29
  5. * @author name of author
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <assert.h>
  19. #include <time.h>
  20. #include <arpa/inet.h>
  21. #include <sys/socket.h>
  22.  
  23. #include "debug.h"
  24. #include "memory.h"
  25. #include "common.h"
  26. #include "server_args.h"
  27.  
  28. #define PORT_MAX 65535
  29.  
  30. void loop_UDP_server(int sock);
  31. int setup_UDP_server(int port);
  32.  
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.     struct gengetopt_args_info args;
  37.  
  38.     // gengetopt parser: deve ser a primeira linha de código no main
  39.     if( cmdline_parser(argc, argv, &args) ){
  40.         ERROR(1, "Erro: execução de cmdline_parser\n");
  41.     }
  42.     int port = args.port_arg;
  43.     if(port < 0 || port > PORT_MAX){
  44.         fprintf(stderr, "error: invalid port %hd\n",port);
  45.         exit(2);
  46.     }
  47.     //Criar socket (1)
  48.     int serv_sock = setup_UDP_server(port);
  49.  
  50.     //Receber do cliente(s) (2)
  51.     loop_UDP_server(serv_sock);
  52.    
  53.    
  54.  
  55.     // gengetopt: libertar recurso
  56.     cmdline_parser_free(&args);
  57.  
  58.     return 0;
  59. }
  60.  
  61. void loop_UDP_server(int sock){
  62.    
  63.     char S[C_MAX_STRING_LEN];
  64.     int flags = 0;
  65.     ssize_t ret_recv;
  66.     struct sockaddr_in remote_addr;
  67.     socklen_t addrlen;
  68.  
  69.     while(1){
  70.         printf("[SERVER] Waiting for clients\n");
  71.         //Receber do cliente
  72.         addrlen = sizeof(remote_addr);
  73.         //socket | buffer | tamanho do buffer -1 <- (/0) | flags | socketaddr | addrlen
  74.         ret_recv = recvfrom(sock, &S, sizeof(S)-1,flags,(struct sockaddr*)&remote_addr, &addrlen);
  75.         //tratar erros
  76.         if(ret_recv == -1){
  77.             fprintf(stderr, "recvfrom error: %s\n",strerror(errno));
  78.             //nao termina mesmo que haja erro pois nao faria sentido terminar pq um cliente deu erro
  79.             continue;
  80.         }
  81.    
  82.         S[ret_recv] = '\0'; //terminador da string
  83.         printf("Server received string(%02d): %s\n",ret_recv,S);
  84.  
  85.         //devolver ao cliente
  86.         ssize_t ret_send = sendto(sock, &S, ret_recv, flags,(struct sockaddr*)&remote_addr,addrlen);
  87.         if(ret_send == -1){
  88.             fprintf(stderr, "Cannot send '%s'to client: %s\n",S,strerror(errno));
  89.             continue;
  90.         }
  91.     }//While
  92. }
  93.  
  94.  
  95.  
  96. int setup_UDP_server(int port){ //(1) Parametro de entrada: porto
  97.    
  98.     //SOCKET
  99.     int serv_sock = socket(AF_INET,SOCK_DGRAM,0);
  100.     if (serv_sock == -1)
  101.         ERROR(2, "Criaçao do socket");
  102.  
  103.     //BIND
  104.     struct sockaddr_in bind_addr;
  105.         //limpeza
  106.     memset(&bind_addr, 0, sizeof(bind_addr));
  107.         //inicializar campos
  108.     bind_addr.sin_family = AF_INET;
  109.         //unsigned to hostlong(formato rede)
  110.     bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  111.         //unsigned to hostshort(formato rede)
  112.     bind_addr.sin_port = htons(port);
  113.         //bind
  114.     if(bind(serv_sock,(struct sockaddr*)&bind_addr,sizeof(bind_addr)) == -1){
  115.         ERROR(4,"Cannot bind to port %d",port);
  116.     }
  117.  
  118.     return serv_sock;
  119. }
RAW Paste Data
Top