-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
173 lines (147 loc) · 3.57 KB
/
server.c
File metadata and controls
173 lines (147 loc) · 3.57 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include<strings.h>
#include<string.h>
#include "protocolFT.h"
int main(int argc, char *argv[])
{
struct sockaddr_in serv_addr, cli_addr;
if(argc<2)
{
fprintf(stderr, "ERROR,no port provided.\n");
exit(1);
}int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
fprintf(stderr, "ERROR,opening socket.\n");
exit(1);
}bzero((char *)&serv_addr, sizeof(serv_addr));
int portno;
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr, "ERROR,binding socket.\n");
exit(1);
}
printf("listening for the client........\n");
listen(sockfd,5);
int clilen;
clilen = sizeof(cli_addr);
int newsockfd;
newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(newsockfd < 0)
{
fprintf(stderr, "ERROR,new socket.\n");
exit(1);
}
char buffer[256];
bzero(buffer, 256);
int n;
n = read(newsockfd, buffer, 255);
if(n < 0)
{
fprintf(stderr, "ERROR,reading from socket.\n");
exit(1);
}
printf("File path received from client: \"%s\"\n",buffer);
FILE *ptrFile;
ptrFile = fopen(buffer,"rb");
if(!ptrFile)
{
fprintf(stderr, "ERROR,opening file.\n");
exit(1);
}
printf("file opening success.\n");
//finding size of file
unsigned long int fileSize;
fseek(ptrFile,0,SEEK_END);
fileSize = ftell(ptrFile);
rewind(ptrFile);
printf("size of file = %d\n",fileSize);
unsigned char *fileArray;
//calloc automatically sets all bytes to zero after allocating
fileArray = (unsigned char*) calloc(fileSize , sizeof(char));
if(!fileArray)
{
printf("memory allocation of fileArray failed!!!\n");
exit(1);
}
unsigned int readSize;
readSize = fread(fileArray, sizeof(char), fileSize ,ptrFile);
if(readSize != fileSize)
{
printf("error in reading of the %s file\n", buffer);
printf("read size = %d\n", readSize);
exit(1);
}
else printf("Reading success!!!\n");
fclose(ptrFile);
//File loading completed
//Starting to send file to client
unsigned long int i=0,j=0;
int packetNo = 1;
int repeatNo = 0;
struct Packet packet;
//making each packets...
for(i = 0; i < fileSize; i += DATA_ARRAY_SIZE )
{
packet.head.fileSize = fileSize;
packet.head.sequenceNo = packetNo;
packet.head.sendNo = repeatNo;
packet.head.dataSize = DATA_ARRAY_SIZE;
for(j = 0; j < DATA_ARRAY_SIZE ; j++)
{
if(i+j<fileSize)
{
packet.dataArray[j] = fileArray[i+j];
}
else if(i+j == fileSize)
{
packet.dataArray[j] = 0;
packet.head.dataSize = j;
}
else
{
packet.dataArray[j] = 0;
}
}
packet.crc[1] = 0;
packet.crc[0] = 0;
crcgenerator(packet.dataArray,packet.head.dataSize,packet.crc);
packetNo++;
repeatNo = 0;
char acknowledgment;
do
{
send(newsockfd,&packet,sizeof(packet),0);
printf("Percentage sent : %.2f%%\r",(float)(i * 100)/ fileSize );
n = read(newsockfd, &acknowledgment, 1);
if(n < 0)
{
fprintf(stderr, "ERROR,reading packet send acknowledge from socket.\n");
exit(1);
}
repeatNo++;
packet.head.sendNo = repeatNo;
}while(acknowledgment == 'f');
}
printf("Percentage sent : 100.00%% \n");
bzero(buffer, 256);
strcpy(buffer,"End of file transmission.");
n = write(newsockfd, buffer, strlen(buffer));
if (n < 0)
{
fprintf(stderr, "ERROR, sending end of file msg to client.\n");
exit(1);
}
printf("Program ends here.\n");
scanf("\n");
return 0;
}