-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrad_client.c
181 lines (145 loc) · 4.15 KB
/
trad_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <argp.h>
#include <signal.h>
#include <fcntl.h>
#define SERVER_PORT 3333
#define READ_SIZE 1024
#define DEFAULT_HOST "127.0.0.1";
#define DEFAULT_PORT "3333";
int num_ops = 10;
void signal_handler(int sig_num) {
printf("Exiting\n");
exit(0);
}
void fatal_error(const char *syscall) {
perror(syscall);
exit(1);
}
void print_addr(struct addrinfo *addr) {
char num_name[255];
char num_serv[255];
int status = getnameinfo(addr->ai_addr, addr->ai_addrlen, num_name, 255, num_serv, 255, NI_NUMERICHOST | NI_NUMERICSERV);
if (status != 0) {
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
fprintf(stderr, "Trying %s:%s\n", num_name, num_serv);
}
int client_connect(const char* host, const char* service) {
int sock;
struct addrinfo hints;
struct addrinfo *result, *rp;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* TCP socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
int status = getaddrinfo(host, service, &hints, &result);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
//print_addr(rp);
sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock == -1)
continue;
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sock);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not connect\n");
exit(EXIT_FAILURE);
}
return sock;
}
void loop(char *host, char *port) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int read_count = 0;
char buffer[READ_SIZE] = { 'Z' };
while (read_count < num_ops) {
int sock = client_connect(host, port);
int status = fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
if (status == -1){
perror("calling fcntl");
}
int bytes = write(sock, buffer, READ_SIZE);
if (bytes < 0) {
perror("write");
close(sock);
read_count += 1;
continue;
}
while (1) {
bytes = read(sock, buffer, READ_SIZE);
if (bytes < 0) {
if (errno == EAGAIN) {
continue;
} else {
perror("read");
break;
}
} else if (bytes == 0) {
break;
}
}
close(sock);
read_count += 1;
}
}
const char argp_program_doc[] =
"echo client\n"
"\n"
"Usage: client\n";
static const struct argp_option opts[] = {
{"host", 'h', "address", 0, "address of host"},
{"port", 'p', "number", 0, "port of echo service"},
{"times", 'n', "number", 0, "number of times"},
{},
};
char *host = DEFAULT_HOST;
char *port = DEFAULT_PORT;
error_t parse_opts (int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'h':
host = arg;
break;
case 'p':
port = arg;
break;
case 'n':
num_ops = atoi(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp argp = {
.options = opts,
.parser = parse_opts,
.doc = argp_program_doc,
};
int main(int argc, char *argv[]) {
int err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return 0;
signal(SIGINT, signal_handler);
// Client
loop(host, port);
return 0;
}