-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcget.c
67 lines (62 loc) · 1.45 KB
/
mcget.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
// mcget.c
// Created by Larry Williamson and Grant Garrett on 12/8/14.
#include "csapp.h"
#define BUF_SIZE 256
int main(int argc, char **argv)
{
/* establish connection */
char *host, filename[80];
int clientfd, port;
unsigned int secretKey, requestType;
if (argc != 5)
{
fprintf(stderr, "usage: %s <host> <port> <secret key> <filename>\n", argv[0]);
exit(0);
}
host = argv[1];
port = atoi(argv[2]);
secretKey = htonl(atoi(argv[3]));
requestType = htonl(0);
strcpy(filename, argv[4]);
clientfd = Open_clientfd(host, port);
/* end connection setup */
/* send parameters to server */
Rio_writen(clientfd, &secretKey, sizeof(unsigned int));
Rio_writen(clientfd, &requestType, sizeof(unsigned int));
// check file before sending to server
FILE *fp = fopen(filename, "w+");
if (fp == NULL)
{
printf("Error opening file\n");
return -1;
}
Rio_writen(clientfd, filename, 80);
/* end parameter send */
/* pull data from server and print */
char buff[BUF_SIZE];
int bytesReceived;
while((bytesReceived = Rio_readn(clientfd, buff, BUF_SIZE)) > 0)
{
fwrite(buff, 1, bytesReceived, fp);
}
rewind(fp);
while (fgets(buff, BUF_SIZE, fp) != NULL)
{
printf("%s", buff);
}
/* end data pull */
/* get return status and end connection */
unsigned int status;
Rio_readn(clientfd, &status, sizeof(unsigned int));
fclose(fp);
/*
if (status != 0)
{
printf("Error\n");
remove(filename);
}
*/
Close(clientfd);
exit(0);
/* connection closed */
}