-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_tcp.c
144 lines (109 loc) · 3.09 KB
/
client_tcp.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
#include "conn.h"
#define MAX_FILENAME 100
#define TOKEN 65536
int requestFile(Connection conn, char * filename) {
char * request;
char header[6];
int str_len = strlen(filename), fd, ctrl;
uint32_t file_dim = 0, timestamp = 0;
uint32_t file_dim_net = 0, timestamp_net = 0;
printf("\trequested file: %s\n", filename);
request = malloc((str_len+6)*sizeof(*request));
sprintf(request, "GET %s\r\n", filename);
ctrl = conn_sends(conn, request);
free(request);
if (ctrl == -1) return -1;
ctrl = conn_recvn(conn, header, 1);
if (ctrl == -1) return -1;
if (*header == '+') {
ctrl = conn_recvs(conn, header+1, 4, "\r\n");
if (ctrl == -1) return -1;
ctrl = conn_recvn(conn, &file_dim_net, sizeof(file_dim));
if (ctrl == -1) return -1;
ctrl = conn_recvn(conn, ×tamp_net, sizeof(timestamp));
if (ctrl == -1) return -1;
file_dim = ntohl(file_dim_net);
timestamp = ntohl(timestamp_net);
printf("Received file dim: %u B\n", file_dim);
printf("Received timestamp: %u\n", timestamp);
fd = open(filename, O_RDWR | O_CREAT);
if (fd == -1) {
report_err("Cannot open the file");
return -1;
}
/* Receive the file */
if (file_dim < TOKEN) {
ctrl = conn_recvfile(conn, fd, file_dim);
if (ctrl == -1) return -1;
} else {
ctrl = conn_recvfile_tokenized(conn, fd, file_dim, TOKEN);
if (ctrl == -1) return -1;
}
} else if (*header == '-') {
ctrl = conn_recvs(conn, header+1, 5, "\r\n");
if (ctrl == -1) return -1;
printf("Server response: [%s]\nTerminate.\n", header);
return -1;
} else {
fprintf(stderr, "Response not recognized, terminate.\n");
return -1;
}
close(fd);
return 0;
}
void signalHandler(int sig) {
if (sig == SIGCHLD) {
/* One of the children is died, but can be that more than one are died */
while (waitpid(-1, NULL, WNOHANG) > 0) {}
}
if (sig == SIGINT) {
/* Some termination before die */
printf("\n");
exit(0);
}
}
int main(int argc, char *argv[]) {
Connection conn;
char filename[MAX_FILENAME];
int ctrl, first = TRUE;
pid_t child;
if (argc != 3) {
fprintf(stderr, "syntax: %s <address> <port>\n", argv[0]);
exit(-1);
}
//if (checkaddress(argv[1])) return -1;
conn = conn_connect(argv[1], checkport(argv[2]));
signal(SIGCHLD, signalHandler);
signal(SIGINT, signalHandler);
printf("Insert filename or <quit> to close the connection\n");
while (TRUE) {
readline(filename, MAX_FILENAME);
if (!strcmp(filename, "Q") || !strcmp(filename, "q")) {
printf("Terminating connection with server...\n");
ctrl = conn_sends(conn, "QUIT\r\n");
if (ctrl == -1) return -1;
ctrl = conn_close(conn);
if (ctrl == -1) return -1;
return 0;
}
if (!strcmp(filename, "A") || !strcmp(filename, "a")) {
printf("Aborting connection with server...\n");
if (kill(child, SIGKILL))
report_err("Cannot terminate a child");
ctrl = conn_close(conn);
if (ctrl == -1) return -1;
return 0;
}
if (!first)
wait(NULL);
else
first = FALSE;
child = fork();
if (!child) {
ctrl = requestFile(conn, filename);
if (ctrl == -1) return -1;
return 0;
}
}
return 0;
}