-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c.bak
90 lines (78 loc) · 2.76 KB
/
server.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
//#include <netdb.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <pthread.h>
int main(int argc, char * argv[]) {
if(argc < 2){
printf("Not enough arguments. System exiting..");
exit(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[1]));
server.sin_addr.s_addr = INADDR_ANY;
//binding
if(bind(sock,(struct sockaddr*) &server, sizeof(server)) < 0) printf("Error binding!!\n");
//recieving handshake
char buffer[1024];
socklen_t fromLength = sizeof(from);
recvfrom(sock, buffer, 1024, 0, (struct sockaddr*)&from, &fromLength);
FILE * file;
if((file = fopen(buffer, "rb"))) {
printf("%s", "File exists!\n");
fseek(file, 0, SEEK_END);
uint16_t fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
uint16_t convertedFileSize = htons(fileSize);
sendto(sock, &convertedFileSize, sizeof convertedFileSize, 0, (struct sockaddr*)&from, fromLength);
}
else {
printf("%s", "File does not exists. Exiting..");
memset(&buffer, 0, sizeof buffer);
strcpy(buffer, "Finished");
sendto(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, fromLength);
exit(0);
}
//file exists. Beginning transmission
/* int stillReading;
while(1) {
memset(&buffer, 0, sizeof buffer);
stillReading = fscanf(file,"%s", buffer);
if(stillReading <= 0){
memset(&buffer, 0, sizeof buffer);
strcpy(buffer, "Finished");
sendto(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, fromLength);
printf("%s", "Server End\n");
fflush(file);
fclose(file);
close(sock);
exit(0);
}
printf("%s", "Server Sent\n");
sendto(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, fromLength);
}*/
while(fread(buffer, 1, sizeof buffer, file)>0) {
sendto(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, fromLength);
memset(&buffer, 0, sizeof buffer);
printf("%s", "Server Sent\n");
}
memset(&buffer, 0, sizeof buffer);
strcpy(buffer, "Finished");
sendto(sock, buffer, sizeof buffer, 0, (struct sockaddr*)&from, fromLength);
printf("%s", "Server End\n");
fflush(file);
fclose(file);
close(sock);
exit(0);
return 0;
}