Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /**
  2. * @file: client.c
  3. * @date: 2016-11-17
  4. * @author: autor
  5. */
  6. #include <stdio.h>
  7. #include <sys/types.h>
  8. #include <arpa/inet.h>
  9. #include <sys/socket.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12.  
  13. #include "memory.h"
  14. #include "debug.h"
  15. #include "common.h"
  16. #include "client_opt.h"
  17.  
  18. #define C_MAX_PORT (1<<16) // ==> common.h
  19. int check_port(int port){
  20. if( port <= 0 || port >= C_MAX_PORT ){
  21. fprintf(stderr,"ERROR: invalid port '%d'. Must be within"
  22. "[1,%d]\n", port, C_MAX_PORT-1);
  23. exit(EXIT_FAILURE);
  24. }
  25. return port;
  26. }
  27.  
  28. #include <sys/time.h>
  29.  
  30. int main(int argc, char *argv[]){
  31. /* Estrutura gerada pelo utilitario gengetopt */
  32. struct gengetopt_args_info args_info;
  33.  
  34. /* Processa os parametros da linha de comando */
  35. if (cmdline_parser (argc, argv, &args_info) != 0){
  36. exit(ERR_ARGS);
  37. }
  38.  
  39. //Receber o porto do args
  40. int remote_port = check_port(args_info.port_arg); //copiar a função check_port
  41.  
  42. /*int time = args_info.timeout_arg;
  43.  
  44. struct timeval tv;
  45. tv.tv_sec = time;
  46. tv.tv_usec = 0;*/
  47.  
  48. // UDP IPv4: cria socket
  49. int udp_client_socket;
  50.  
  51.  
  52. if ((udp_client_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  53. ERROR(31, "Can't create udp_server_socket (IPv4)");
  54.  
  55. /*if (setsockopt(udp_client_socket,SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)) == -1)
  56. ERROR(31, "Failed setsockopt\n");*/
  57.  
  58. // UDP IPv4: informação do servidor UDP
  59. socklen_t udp_server_endpoint_length = sizeof(struct sockaddr_in);
  60. struct sockaddr_in udp_server_endpoint;
  61. //Preencher a estrutura
  62. memset(&udp_server_endpoint, 0, sizeof(struct sockaddr_in));
  63. udp_server_endpoint.sin_family = AF_INET;
  64. udp_server_endpoint.sin_port = htons(remote_port);
  65. switch (inet_pton(AF_INET, args_info.ip_arg , &udp_server_endpoint.sin_addr.s_addr)) {
  66. case 0:
  67. fprintf(stderr,"[ERROR] Invalid IPv4 '%s'\n", args_info.ip_arg);
  68. exit(EXIT_FAILURE);
  69. case -1:
  70. ERROR(EXIT_FAILURE,"Invalid family '%s'", args_info.ip_arg);
  71. }
  72. printf("[CLIENT] Attempting to connect to server\n");
  73.  
  74. printf("\n\n\t\tPING\n\n");
  75.  
  76. // aqui... a comunicação com o servidor
  77. // UDP IPv4: variáveis auxiliares para sendto() / recvfrom()
  78. ssize_t udp_read_bytes, udp_sent_bytes;
  79. char buffer[6] = "PING";
  80. //nao esquecer de usar a função htons no que se pretende enviar
  81.  
  82. // UDP IPv4: "sendto" para o servidor
  83. printf("a enviar dados para o servidor... "); fflush(stdout);
  84. if ((udp_sent_bytes = sendto(udp_client_socket, buffer, strlen(buffer), 0, (struct sockaddr *) &udp_server_endpoint, udp_server_endpoint_length)) == -1)
  85. ERROR(24, "Can't sendto server");
  86. printf("ok. (%d bytes sent)\n", (int)udp_sent_bytes);
  87.  
  88. int tries = 0;
  89. do{
  90. // UDP IPv4: "recvfrom" do servidor (bloqueante)
  91. //printf("à espera de dados do servidor... "); fflush(stdout);
  92. if ((udp_read_bytes = recvfrom(udp_client_socket, buffer, sizeof(buffer), MSG_DONTWAIT, (struct sockaddr *) &udp_server_endpoint, &udp_server_endpoint_length)) > 0){
  93. printf("ok. (%d bytes received)\n", (int)udp_read_bytes);
  94. printf("\n\n\t\t%s\n\n", buffer);
  95. }else{
  96. printf("\nTries: %d\n", tries+1);
  97. tries ++;
  98. sleep(2);}
  99. }while(tries<3 && udp_read_bytes < 0);
  100.  
  101. if(tries == 3){
  102. printf("Can't received nothing!\n");
  103. }
  104. //nao esquecer de usar a funçõa ntohs no que se recebe
  105.  
  106.  
  107. // UDP IPv4: fecha socket (client)
  108. if (close(udp_client_socket) == -1)
  109. ERROR(23, "Can't close udp_client_socket (IPv4)");
  110.  
  111. cmdline_parser_free(&args_info);
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement