-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.c
82 lines (65 loc) · 1.76 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
#include "dtp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/stat.h>
/* Returns size of file. */
size_t get_filesize(const char* filename) {
struct stat file_stats;
stat(filename, &file_stats);
return file_stats.st_size;
}
int main (int argc, char *argv[]) {
if( argc != 4 ) {
fprintf(stderr, "Usage: %s <server_ip> <server_port> <filename>\n", argv[0]);
return 1;
}
const char* server_ip = argv[1];
port_t server_port = atoi(argv[2]);
int stat;
dtp_client client;
stat = init_dtp_client(&client, server_ip, server_port);
if( stat < 0 ) {
perror("Init client :");
return 1;
}
stat = dtp_connect(&client);
if( stat < 0 ) {
perror("Connect :");
return 1;
}
printf("Client connected to server from port %u\n",
ntohs(client.self.sin_port));
printf("(%d) :: %s:%u\n", client.status,
inet_ntoa(client.addr.sin_addr), ntohs(client.addr.sin_port));
printf("InitSeq <Self : %u, Remote : %u>\n", client.seqno, client.ackno);
FILE* file = fopen(argv[3], "rb");
if( file == NULL ) {
perror("fopen");
return 1;
}
size_t filesize = get_filesize(argv[3]);
if( dtp_send(&client, &filesize, sizeof(size_t)) != 0 ) {
fprintf(stderr, "Error in dtp_send.\n");
return 1;
}
printf("Sent file size : %lu\n", filesize);
size_t chksize;
dtp_recv(&client, &chksize, sizeof(size_t));
printf("File size acked : %lu\n", chksize);
const size_t BUFLEN = 1500;
char buff[BUFLEN];
while( 1 ) {
size_t bytes = fread(buff, 1, BUFLEN, file);
if( bytes == 0 )
break;
if( dtp_send(&client, buff, bytes) != 0 ) {
fprintf(stderr, "Error in dtp_send.\n");
return 1;
}
}
fclose(file);
close_dtp_gate(&client);
return 0;
}