-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
91 lines (76 loc) · 1.95 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
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include "common_socket.h"
#include "client_file_sender.h"
#define MAX_LEN_FILENAME 100
#define MAX_LEN_BUF 2000
bool resive(struct socket* socket) {
int status = 0;
int received = 0;
char buf[MAX_LEN_BUF];
while ( true ) {
status = socket_receive_some(socket, &buf[received], \
MAX_LEN_BUF - received - 1);
if (status < 0) { //socker error
return false;
} else if (status == 0) {
break;
} else {
received = status;
buf[received] = 0;
printf("%s", buf);
received = 0;
}
}
printf("\n");
return true;
}
int main(int argc, char* argv[]) {
if (argc != 3 && argc !=4) {
fprintf(stderr, "Uso:\n./client <direccion> <puerto> [<input>]\n");
return 1;
}
char filename[MAX_LEN_FILENAME];
if (argc == 3) {
char* status = fgets(filename, MAX_LEN_FILENAME, stdin);
filename[strlen(filename) -1] = '\0';
if (!status) {
return 1;
}
} else {
snprintf(filename, MAX_LEN_FILENAME, "%s", argv[3]);
}
struct file_sender fs;
char* host = argv[1];
char* port = argv[2];
struct socket socket;
socket_create(&socket, host, port);
if (!socket_start(&socket)) {
socket_destroy(&socket);
return 1;
}
if (!socket_connect_with_server(&socket)) {
socket_destroy(&socket);
return 1;
}
int status = 0;
file_sender_create(&fs, filename, &socket);
if (!file_sender_start(&fs)){
status = 1;
}
socket_disables_send_operations(&socket);
if ( !resive(&socket) ) {
status = 1;
}
socket_destroy(&socket);
file_sender_destroy(&fs);
return status;
}