daily pastebin goal
79%
SHARE
TWEET

Pa_setup_server_PL5

a guest Dec 14th, 2017 77 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 MAX_PORT    (1 << 16)
  29.  
  30. int setup_server(int port, int type, int backlog);
  31. int validate_port_number(int port);
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.     struct gengetopt_args_info args;
  36.  
  37.     // gengetopt parser: deve ser a primeira linha de código no main
  38.     if( cmdline_parser(argc, argv, &args) ){
  39.         ERROR(1, "Erro: execução de cmdline_parser\n");
  40.     }
  41.  
  42.     int port = validate_port_number(args.port_arg);
  43.  
  44.     int backlog = 0;
  45.     int sock = setup_server(port, SOCK_DGRAM, backlog);
  46.  
  47.     close(sock);
  48.  
  49.     // gengetopt: libertar recurso
  50.     cmdline_parser_free(&args);
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. int validate_port_number(int port){
  57.     if( port < 0 || port >= MAX_PORT ){ // antes estava >
  58.         fprintf(stderr,"invalid port number (%d)\n", port);
  59.         exit(5);
  60.     }
  61.     return port;
  62. }
  63.  
  64. /* @return socket already binded locally */
  65. int setup_server(int port, int type, int backlog){
  66.     assert( port > 0 || port < MAX_PORT );
  67.     assert(type==SOCK_DGRAM || type==SOCK_STREAM);
  68.     assert( (type==SOCK_STREAM && backlog > 0) || (type==SOCK_DGRAM));
  69.  
  70.     int sock = socket(AF_INET, type, 0);
  71.     if( sock == -1 ){
  72.         ERROR(1,"Cannot create socket");
  73.     }
  74.  
  75.  
  76.     struct sockaddr_in  my_addr;
  77.     memset(&my_addr,0,sizeof(my_addr));
  78.     my_addr.sin_family = AF_INET;
  79.     my_addr.sin_port = htons(port);
  80.     my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  81.  
  82.     int ret_bind=bind(sock,(struct sockaddr*)&my_addr,sizeof(my_addr));
  83.     if( ret_bind == -1 ){
  84.         ERROR(2,"Cannot bind socket (port=%d)\n", port);
  85.     }
  86.  
  87.     if( type == SOCK_STREAM ){
  88.         int ret_listen = listen(sock, backlog);
  89.         if( ret_listen == -1 ){
  90.             ERROR(3,"Cannot listen");
  91.         }
  92.     }
  93.  
  94.     printf("[SERVER] bind port %d\n", port);
  95.     return sock;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top