Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file: client.c
- * @date: 2016-11-17
- * @author: autor
- */
- #include <stdio.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <string.h>
- #include "memory.h"
- #include "debug.h"
- #include "common.h"
- #include "client_opt.h"
- #define C_MAX_PORT (1<<16) // ==> common.h
- int check_port(int port){
- if( port <= 0 || port >= C_MAX_PORT ){
- fprintf(stderr,"ERROR: invalid port '%d'. Must be within"
- "[1,%d]\n", port, C_MAX_PORT-1);
- exit(EXIT_FAILURE);
- }
- return port;
- }
- #include <sys/time.h>
- int main(int argc, char *argv[]){
- /* Estrutura gerada pelo utilitario gengetopt */
- struct gengetopt_args_info args_info;
- /* Processa os parametros da linha de comando */
- if (cmdline_parser (argc, argv, &args_info) != 0){
- exit(ERR_ARGS);
- }
- //Receber o porto do args
- int remote_port = check_port(args_info.port_arg); //copiar a função check_port
- /*int time = args_info.timeout_arg;
- struct timeval tv;
- tv.tv_sec = time;
- tv.tv_usec = 0;*/
- // UDP IPv4: cria socket
- int udp_client_socket;
- if ((udp_client_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- ERROR(31, "Can't create udp_server_socket (IPv4)");
- /*if (setsockopt(udp_client_socket,SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)) == -1)
- ERROR(31, "Failed setsockopt\n");*/
- // UDP IPv4: informação do servidor UDP
- socklen_t udp_server_endpoint_length = sizeof(struct sockaddr_in);
- struct sockaddr_in udp_server_endpoint;
- //Preencher a estrutura
- memset(&udp_server_endpoint, 0, sizeof(struct sockaddr_in));
- udp_server_endpoint.sin_family = AF_INET;
- udp_server_endpoint.sin_port = htons(remote_port);
- switch (inet_pton(AF_INET, args_info.ip_arg , &udp_server_endpoint.sin_addr.s_addr)) {
- case 0:
- fprintf(stderr,"[ERROR] Invalid IPv4 '%s'\n", args_info.ip_arg);
- exit(EXIT_FAILURE);
- case -1:
- ERROR(EXIT_FAILURE,"Invalid family '%s'", args_info.ip_arg);
- }
- printf("[CLIENT] Attempting to connect to server\n");
- printf("\n\n\t\tPING\n\n");
- // aqui... a comunicação com o servidor
- // UDP IPv4: variáveis auxiliares para sendto() / recvfrom()
- ssize_t udp_read_bytes, udp_sent_bytes;
- char buffer[6] = "PING";
- //nao esquecer de usar a função htons no que se pretende enviar
- // UDP IPv4: "sendto" para o servidor
- printf("a enviar dados para o servidor... "); fflush(stdout);
- if ((udp_sent_bytes = sendto(udp_client_socket, buffer, strlen(buffer), 0, (struct sockaddr *) &udp_server_endpoint, udp_server_endpoint_length)) == -1)
- ERROR(24, "Can't sendto server");
- printf("ok. (%d bytes sent)\n", (int)udp_sent_bytes);
- int tries = 0;
- do{
- // UDP IPv4: "recvfrom" do servidor (bloqueante)
- //printf("à espera de dados do servidor... "); fflush(stdout);
- if ((udp_read_bytes = recvfrom(udp_client_socket, buffer, sizeof(buffer), MSG_DONTWAIT, (struct sockaddr *) &udp_server_endpoint, &udp_server_endpoint_length)) > 0){
- printf("ok. (%d bytes received)\n", (int)udp_read_bytes);
- printf("\n\n\t\t%s\n\n", buffer);
- }else{
- printf("\nTries: %d\n", tries+1);
- tries ++;
- sleep(2);}
- }while(tries<3 && udp_read_bytes < 0);
- if(tries == 3){
- printf("Can't received nothing!\n");
- }
- //nao esquecer de usar a funçõa ntohs no que se recebe
- // UDP IPv4: fecha socket (client)
- if (close(udp_client_socket) == -1)
- ERROR(23, "Can't close udp_client_socket (IPv4)");
- cmdline_parser_free(&args_info);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement