Repository navigation
Expand file tree
/
Copy pathclient.c
More file actions
139 lines (115 loc) · 3.55 KB
/
Copy pathclient.c
File metadata and controls
139 lines (115 loc) · 3.55 KB
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "rxb.h"
#include "utils.h"
#ifdef USE_LIBUNISTRING
#include <unistr.h>
#endif
#define MAX_REQUEST_SIZE (64 * 1024)
int main(int argc, char **argv) {
int err, sd, i = 1;
struct addrinfo hints, *res, *ptr;
char *hostname = argv[1];
char *port = argv[2];
if (argc != 3) {
printf("use: %s <hostname> <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
signal(SIGPIPE, SIG_IGN);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((err = getaddrinfo(hostname, port, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}
for(ptr = res; ptr != NULL; ptr = ptr->ai_next) {
if((sd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) < 0) {
printf("connection FAILED at attempt %d\n", i);
continue;
}
if((err = connect(sd, ptr->ai_addr, ptr->ai_addrlen)) == 0) {
printf("connection ACCEPTED at attempt %d\n", i);
break;
}
i++;
close(sd);
}
if(ptr == NULL) {
fprintf(stderr, "connection failed.\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(res);
rxb_t rxb;
for(;;) {
rxb_init(&rxb, MAX_REQUEST_SIZE * 2);
char mese[1024], tipo[1024], n_res[32];
puts("mese ('fine' per terminare):");
if(fgets(mese, sizeof(mese), stdin) < 0) {
perror("fgets");
exit(EXIT_FAILURE);
}
if(strcmp(mese, "fine\n") == 0) break;
puts("tipo ('fine' per terminare):");
if(fgets(tipo, sizeof(tipo), stdin) < 0) {
perror("fgets");
exit(EXIT_FAILURE);
}
if(strcmp(mese, "fine\n") == 0) break;
puts("n_res ('fine' per terminare):");
if(fgets(n_res, sizeof(n_res), stdin) < 0) {
perror("fgets");
exit(EXIT_FAILURE);
}
if(strcmp(n_res, "fine\n") == 0) break;
// send request to the server
if(write_all(sd, mese, strlen(mese)) < 0) {
perror("write_all");
exit(EXIT_FAILURE);
}
if(write_all(sd, tipo, strlen(tipo)) < 0) {
perror("write_all");
exit(EXIT_FAILURE);
}
if(write_all(sd, n_res, strlen(n_res)) < 0) {
perror("write_all");
exit(EXIT_FAILURE);
}
for(;;) {
char line[MAX_REQUEST_SIZE];
size_t line_len;
memset(line, 0, sizeof(line));
line_len = sizeof(line) - 1;
if(rxb_readline(&rxb, sd, line, &line_len) < 0) {
perror("rxb_readline");
rxb_destroy(&rxb);
close(sd);
exit(EXIT_FAILURE);
}
#ifdef USE_LIBUNISTRING
if(u8_check((uint8_t *) line, line_len) != NULL) {
fprintf(stderr, "not a valid utf-8 string!\n");
rxb_destroy(&rxb);
close(sd);
exit(EXIT_FAILURE);
}
#endif
puts(line);
if(strcmp(line, "----- END REQUEST -----") == 0) break;
}
}
rxb_destroy(&rxb);
close(sd);
return 0;
}