-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.c
66 lines (51 loc) · 1.48 KB
/
Server.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
#include "dtp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char *argv[]) {
if( argc != 2 ) {
fprintf(stderr, "Usage: %s <server_port>\n", argv[0]);
return 1;
}
char client_ip[1<<5];
port_t client_port, server_port = atoi(argv[1]);
int stat;
dtp_server server;
stat = init_dtp_server(&server, server_port);
if( stat < 0 ) {
perror("Init client :");
return 1;
}
printf("Server listening on port %u\n", ntohs(server.self.sin_port));
stat = dtp_listen(&server, client_ip, &client_port);
if( stat < 0 ) {
perror("Connect :");
return 1;
}
printf("(%d) :: %s:%u\n", server.status, client_ip, client_port);
printf("InitSeq <Self : %u, Remote : %u>\n", server.seqno, server.ackno);
size_t filesize;
dtp_recv(&server, &filesize, sizeof(size_t));
printf("Received file size : %lu\n", filesize);
if( dtp_send(&server, &filesize, sizeof(size_t)) != 0 ) {
fprintf(stderr, "Error in dtp_send.\n");
return 1;
}
size_t remsize = filesize;
FILE* outfile = fopen("Outfile", "wb");
const size_t BUFLEN = 1200;
char buff[BUFLEN];
while( remsize > 0 ) {
size_t bytes = 0;
bytes = dtp_recv(&server, buff, (BUFLEN < remsize ? BUFLEN : remsize));
fwrite(buff, 1, bytes, outfile);
remsize -= bytes;
printf("Bytes remaining :\t%10lu\r", remsize);
fflush(stdout);
}
printf("\nOutfile received.\n");
fclose(outfile);
close_dtp_gate(&server);
return 0;
}