-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
258 lines (221 loc) · 5.63 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <netdb.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <wordexp.h>
#include <sysexits.h>
#include "common.h"
#include "client.h"
int debug = 0;
void usage(char *program_name)
{
printf("Usage: %s [-d] -s <server> -p <port>\n", program_name);
}
void f_put (char *title, char *xLabel, char *yLabel, char *style, char *src, int sd)
{
struct put petition;
petition.code = PUT_CODE;
strncpy(petition.title, title, 20);
strncpy(petition.xLabel, xLabel, 20);
strncpy(petition.yLabel, yLabel, 20);
strncpy(petition.style,style, 20);
strncpy(petition.src, src, 20);
int bytes_written;
if((bytes_written = write(sd, (char *) &petition, sizeof(struct put))) == -1) {
perror("c> Error sending put to server.\n");
return;
}
//Send plot data file to server
FILE *file;
if ((file = fopen(src, "rb")) == 0){
perror("c> Error opening file.\n");
return;
}
fseek(file, 0L, SEEK_END);
int size = ftell(file);
fseek(file, 0L, SEEK_SET);
char filesize[10];
sprintf(filesize, "%d", size);
printf("c> File size - %d\n", size);
if((bytes_written = send(sd, filesize, 10, 0)) == -1) {
perror("s> Could not send file size to server.\n");
fclose(file);
return;
}
fclose(file);
if ((sendfile(sd, open(src, 0), 0, size)) == -1){
perror("c> Error sending file.\n");
fclose(file);
return;
}
// Read ack
char bytes[1];
ssize_t bytes_read;
if((bytes_read = read(sd, bytes, 1)) == -1) {
perror("c> Error receiving data from server.\n");
return;
} else if(bytes_read == 0) {
printf("c> Server has closed the connection.\n");
close(sd);
exit(EXIT_SUCCESS);
}
if(bytes[0] == PUT_CODE) {
printf("c> OK\n");
}
return;
}
void f_get(char *dst, int sd)
{
struct get petition;
petition.code = GET_CODE;
strncpy(petition.file, dst, 30);
int bytes_written;
if((bytes_written = write(sd, (char *) &petition, sizeof(struct put))) == -1) {
perror("c> Error sending get to server.\n");
return;
}
char size[10];
ssize_t bytes_read;
if((bytes_read = read(sd, size, 10)) == -1) {
perror("c> Error receiving data from server.\n");
return;
} else if(bytes_read == 0) {
printf("c> Server has closed the connection.\n");
close(sd);
exit(EXIT_SUCCESS);
}
int filesize = atoi(size);
printf("c> File size - %d\n", filesize);
char file_data[atoi(size)];
if((bytes_read = read(sd, file_data, filesize)) == -1) {
perror("c> Error receiving data from server.\n");
return;
} else if(bytes_read == 0) {
printf("c> Server has closed the connection.\n");
close(sd);
exit(EXIT_SUCCESS);
}
FILE *file = fopen(dst, "wb");
fwrite(file_data, filesize, 1, file);
fclose(file);
return;
}
void shell(int sd)
{
char line[1024];
char *pch;
int exit = 0;
wordexp_t p;
char **w;
int ret;
memset(&p, 0, sizeof(wordexp_t));
do
{
fprintf(stdout, "c> ");
memset(line, 0, 1024);
pch = fgets(line, 1024, stdin);
if ((strlen(line)>1) && ((line[strlen(line)-1]=='\n') || (line[strlen(line)-1]=='\r')))
line[strlen(line) - 1] = '\0';
ret = wordexp((const char *)line, &p, 0);
if(ret==0)
{
w = p.we_wordv;
if((w != NULL) && (p.we_wordc > 0))
{
if(strcmp(w[0], "quit") == 0)
{
exit = 1;
}else if(strcmp(w[0], "put") == 0){
if(p.we_wordc == 6){
f_put(w[1], w[2], w[3], w[4], w[5], sd);
}else{
printf("Syntax error. Usage: put <title> <xLabel> <yLabel> <style> <file_path>\n");
}
}else if(strcmp(w[0], "get") == 0){
if(p.we_wordc == 2){
f_get(w[1], sd);
}else{
printf("Syntax error. Usage: get <file_path>\n");
}
}else{
fprintf(stderr, "Error: command '%s' not valid.\n", w[0]);
}
}
wordfree(&p);
}
}while ((pch != NULL) && (!exit));
}
int main(int argc, char *argv[])
{
char *program_name = argv[0];
int opt, port = 0;
char *server, *port_s;
while((opt = getopt(argc, argv, "ds:p:")) != -1)
{
switch(opt)
{
case 'd':
debug = 1;
break;
case 's':
server = optarg;
if(debug) printf("Server %s\n", server);
break;
case 'p':
port_s = optarg;
port = strtol(optarg, NULL, 10);
if(debug) printf("Port %s\n", port_s);
break;
case '?':
if((optopt == 's') || (optopt == 'p'))
fprintf(stderr, "Option -%c requieres an argument.\n", optopt);
else if (isprint (optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
default:
usage(program_name);
exit(EX_USAGE);
}
}
if((port < 1024) || (port > 65535))
{
fprintf(stderr, "Error: Port must be in the range 1024 <= port <= 65535");
usage(program_name);
exit(EX_USAGE);
}
if(debug)
{
printf("SERVER: %s PORT: %d\n", server, port);
}
int sd;
struct sockaddr_in address;
struct hostent *hp;
hp = gethostbyname(server);
if(hp == NULL) {
address.sin_addr.s_addr = inet_addr(server);
}
else {
memcpy(&address.sin_addr.s_addr,*(hp->h_addr_list),sizeof(address.sin_addr.s_addr));
}
address.sin_family = AF_INET;
address.sin_port = htons(port);
if((sd = socket(address.sin_family, SOCK_STREAM, TCP)) == -1) {
printf("c> Error connecting to server %s:%d.\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
return -1;
}
if(connect(sd, (struct sockaddr *) &address, sizeof(address)) == -1) {
printf("c> Error connecting to server %s:%d.\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
return -1;
}
shell(sd);
exit(EXIT_SUCCESS);
}