-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c.bak
129 lines (93 loc) · 3.04 KB
/
client.c.bak
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <libgen.h>
//#include "sendrecieve.h"
//#include <time.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <net/if.h>
//#include <pthread.h>
int main(int argc, char *argv[]) {
if(argc < 3) {
printf("Not enough arguments. System exiting..");
exit(1);
}
//hostname resolution
struct in_addr hostIp;
inet_pton(AF_INET,argv[1], &hostIp.s_addr);
struct hostent *host = gethostbyaddr(&hostIp, sizeof hostIp, AF_INET);
if(host == NULL){
printf("Could not verify host. Exiting..\n");
exit(1);
}
char hostName[256];
gethostname(hostName, sizeof hostName);
printf("Host Name: %s and host IP: %s\n", hostName, inet_ntoa(*((struct in_addr *)host->h_addr_list[0])));
//socket creation
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) printf("Could not open socket!!\n");
//host to server setup
struct sockaddr_in server, from;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
//finding our own ip
struct ifaddrs *networkInterfaces, *temp;
getifaddrs(&networkInterfaces);
temp = networkInterfaces;
struct sockaddr_in *ourIPInfo;
while(temp) {
if(temp->ifa_addr && temp->ifa_addr->sa_family == AF_INET){
ourIPInfo = (struct sockaddr_in *) temp->ifa_addr;
}
temp = temp->ifa_next;
}
uint32_t clientIP = ourIPInfo->sin_addr.s_addr; //found our IP
//requesting file
char filename[1024];
strcpy(filename, argv[3]);
unsigned int length = sizeof(struct sockaddr_in);
int fileRequestSent = sendto(sock, filename, 1024,0,(struct sockaddr*)&server, sizeof(server));
if(fileRequestSent < 0) {
printf("Failed to send file request! Exiting..\n");
exit(1);
}
int bufferSize = 1024;
char buffer [bufferSize];
memset(&buffer, 0, sizeof buffer);
recvfrom(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, &length);
if(strcmp(buffer, "Finished") == 0) {
printf("%s", "File does not exists. Exiting..");
exit(0);
}
else{
uint16_t fileSize;
memcpy(&fileSize, buffer, sizeof(fileSize));
uint16_t convertedFileSize = ntohs(fileSize);
printf("%s", "File exists! File size is: ");
printf("%d\n", convertedFileSize);
}
//response from server regarding if the file exists
char * fileName;
fileName = basename(argv[3]);
FILE * file;
file = fopen(fileName,"wb");
while(1) {
memset(&buffer, 0, sizeof buffer);
recvfrom(sock, buffer, 1024, 0, (struct sockaddr*)&from, &length);
if(strcmp(buffer, "Finished") == 0)
break;
fwrite(buffer,sizeof buffer, 1, file);
fflush(stdout);
printf("%s", "Client Recieved\n");
}
fflush(file);
fclose(file);
close(sock);
return 0;
}