/
client.c
151 lines (124 loc) · 3.3 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#define _GNU_SOURCE
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ssocket/ssocket.h"
#include <getopt.h>
//Definiciones
#define BUF_SIZE 512
#define FIRST_PORT 1820
//Variables
int first_pack = 0;
struct timeval dateInicio, dateFin;
char buf[BUF_SIZE];
char* IP_DEST = "";
int mostrarInfo = 0;
int intensiveMode = 0;
int MAX_PACKS = 0;
int DESTINATION_PORT = FIRST_PORT;
double segundos;
void print_usage(){
printf("Uso: ./client [verbose] [intensive] --packets <num> --ip <ip-address> --port <num>\n");
}
void print_config(){
printf("Detalles de la prueba:\n");
printf("\tPaquetes a enviar:\t%d\n", MAX_PACKS);
printf("\tIP de destino:\t%s\n", IP_DEST);
printf("\tPuerto a enviar:\t%d\n", DESTINATION_PORT);
if(intensiveMode) printf("\tModo Intensivo\n") ;
}
void parseArgs(int argc, char **argv){
int c;
int digit_optind = 0;
while (1){
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"packets", required_argument, 0, 'd'},
{"ip", required_argument, 0, 'i'},
{"port", required_argument, 0, 'p'},
{"verbose", no_argument, 0, 'v'},
{"intensive", no_argument, 0, 'f'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "vfd:i:p:",
long_options, &option_index);
if (c == -1)
break;
switch (c){
case 'v':
printf ("Modo Verboso\n");
mostrarInfo = 1;
break;
case 'f':
printf ("Modo Intensivo\n");
intensiveMode = 1;
break;
case 'd':
MAX_PACKS = atoi(optarg);
break;
case 'i':
IP_DEST = optarg;
break;
case 'p':
DESTINATION_PORT = atoi(optarg);
break;
default:
printf("Error: La función getopt_long ha retornado un carácter desconocido. El carácter es = %c\n", c);
print_usage();
exit(1);
}
}
}
int main(int argc, char **argv) {
// Paso 1.- Parsear Argumentos
parseArgs(argc, argv);
// Paso 2.- Validar Argumentos
if(MAX_PACKS < 1 || strlen(IP_DEST)==0) {
printf("Error en el ingreso de parametros\n");
print_usage();
exit(1);
}
if(mostrarInfo) print_config();
// Paso 3.- Llenar Buffer con datos para mandar
int i;
for(i = 0; i < BUF_SIZE; i++)
buf[i] = 'p'+i;
// Paso 4.- Crear Socket
int socket_fd;
char ports[10];
sprintf(ports, "%d", DESTINATION_PORT);
socket_fd = udp_connect(IP_DEST, ports);
if(socket_fd < 0) {
fprintf(stderr, "connection refused\n");
exit(1);
}
//Medir Inicio
gettimeofday(&dateInicio, NULL);
// Paso 4.1.- Escribir en el socket
for(i = 0; i < MAX_PACKS; i++){
if(intensiveMode){
write(socket_fd, buf, BUF_SIZE);
}else{
if(write(socket_fd, buf, BUF_SIZE) != BUF_SIZE) {
gettimeofday(&dateFin, NULL);
segundos = (dateFin.tv_sec+dateFin.tv_usec/1000000.)-(dateInicio.tv_sec*1.0+dateInicio.tv_usec/1000000.);
//fprintf(stderr, "Falla el write al servidor, envio %d paquetes\n", i);
//fprintf(stderr, "total time = %g\n", segundos);
break;
}
}
}
//Medir Fin
gettimeofday(&dateFin, NULL);
segundos=(dateFin.tv_sec*1.0+dateFin.tv_usec/1000000.)-(dateInicio.tv_sec*1.0+dateInicio.tv_usec/1000000.);
if(mostrarInfo){
printf("Tiempo Total = %g\n", segundos);
printf("QPS = %g\n", MAX_PACKS*1.0/segundos);
}else{
printf("%g \n", segundos);
}
exit(0);
return 0;
}