Client 0
Client 0
c 1
1 #define _POSIX_C_SOURCE 200809L
2 #include <netdb.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <signal.h>
8 #ifdef USE_LIBUNISTRING
9 # include <unistr.h> /* per libunistring */
10 #endif
11 #include "rxb.h"
12 #include "utils.h"
13
14 #define MAX_REQUEST_SIZE (64 * 1024)
15
16 int main(int argc, char** argv){
17 int err;
18 struct addrinfo hints, *res, *ptr;
19 char *host_remoto;
20 char *servizio_remoto;
21 int sd, i = 1;
22 rxb_t rxb;
23
24 /* Controllo argomenti */
25 if (argc < 3) {
26 printf("Uso: controllo_conto_corrente <server> <porta>\n" );
27 exit(EXIT_FAILURE);
28 }
29
30 /* Ignoro SIGPIPE */
31 signal(SIGPIPE, SIG_IGN);
32
33 /* Costruzione dell'indirizzo */
34 memset(&hints, 0, sizeof(hints));
35 hints.ai_family = AF_UNSPEC;
36 hints.ai_socktype = SOCK_STREAM;
37
38 /* Risoluzione dell'host */
39 host_remoto = argv[1];
40 servizio_remoto = argv[2];
41 if ((err = getaddrinfo(host_remoto, servizio_remoto, &hints,
&res)) != 0) {
42 fprintf(stderr, "Errore risoluzione nome: %s\n" , gai_strerror
(err));
43 exit(EXIT_FAILURE);
44 }
45
46 for (ptr = res; ptr != NULL; ptr = ptr->ai_next){
47 /*se socket fallisce salto direttamente alla prossima
iterazione*/
48 if ((sd = socket(ptr->ai_family, ptr->ai_socktype, ptr-
>ai_protocol)) < 0){
49 fprintf(stderr,"creazione socket fallita\n" );
C:\Users\dinar\Downloads\client-td-connreuse.c 2
50 continue;
51 }
52 /*se connect funziona esco dal ciclo*/
53 if (connect(sd, ptr->ai_addr, ptr->ai_addrlen) == 0){
54 printf("connect riuscita al tentativo %d\n" ,i);
55 break;
56 }
57 i++;
58 close(sd);
59 }
60
61 /* Verifica sul risultato restituito da getaddrinfo */
62 if (ptr == NULL) {
63 fprintf(stderr, "Errore risoluzione nome: nessun indirizzo
corrispondente trovato\n" );
64 exit(EXIT_FAILURE);
65 }
66
67 /* Liberiamo la memoria allocata da getaddrinfo() */
68 freeaddrinfo(res);
69
70 /* Inizializzo buffer di ricezione */
71 rxb_init(&rxb, MAX_REQUEST_SIZE);
72
73 for (;;) {
74 char categoria[4096];
75
76 /* Lettura dati dall'utente */
77 if (fgets(categoria, sizeof(categoria), stdin) == NULL) {
78 perror("fgets");
79 exit(EXIT_FAILURE);
80 }
81
82 /* Esco se l'utente digita . */
83 if (strcmp(categoria, "fine\n") == 0) {
84 break;
85 }
86
87 /* Invio richiesta al Server */
88 if (write_all(sd, categoria, strlen(categoria)) < 0) {
89 perror("write");
90 exit(EXIT_FAILURE);
91 }
92
93 /* Leggo la risposta del server e la stampo a video */
94 for (;;) {
95 char response[MAX_REQUEST_SIZE];
96 size_t response_len;
97
98 memset(response, 0, sizeof(response));
99 response_len = sizeof(response)-1;
100
101 /* Ricezione risultato */
C:\Users\dinar\Downloads\client-td-connreuse.c 3
102 if (rxb_readline(&rxb, sd, response, &response_len) < 0) {
103 rxb_destroy(&rxb);
104 fprintf(stderr, "Connessione chiusa dal server!\n" );
105 exit(EXIT_FAILURE);
106 }
107
108 #ifdef USE_LIBUNISTRING
109 /* Verifico che il testo sia UTF -8 valido */
110 if (u8_check((uint8_t *)response, response_len) != NULL) {
111 fprintf(stderr, "Response is not valid UTF -8!\n");
112 close(sd);
113 exit(EXIT_FAILURE);
114 }
115 #endif
116
117 puts(response);
118
119 /* Passo a nuova richiesta una volta terminato input Server
*/
120 if (strcmp(response, "--- END REQUEST ---") == 0) {
121 break;
122 }
123 }
124 }
125 /* chiudo socket */
126 close(sd);
127
128 return 0;
129 }
130