Skip to content

Commit 0a64878

Browse files
committed
server sends segmented pdf file
Signed-off-by: Azmah-Bad <[email protected]>
1 parent 0bc2c6d commit 0a64878

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

assets/mock.pdf

104 KB
Binary file not shown.

src/.DS_Store

0 Bytes
Binary file not shown.

src/common/common.c

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ int isReceive(char *expected, int mSocketUDP, struct sockaddr_in * clientAddress
2323
}
2424
return 0;
2525
}
26+

src/server/server.c

+49
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ int main(int argc, char const *argv[])
6969
return 1;
7070
}
7171

72+
sendFile(mSocketUDP, &clientAddress, clientLengthUDP);
73+
7274
if ((recvfrom(mSocketUDP, userInput, RCVSIZE, 0, (struct sockaddr *)&clientAddress, (socklen_t *)&clientLengthUDP)) == -1)
7375
{
7476
perror("recvfrom failed \n");
@@ -116,3 +118,50 @@ int handshake(int mSocketUDP, struct sockaddr_in *clientAddress, int clientLengt
116118

117119
return 1;
118120
}
121+
122+
int sendFile(int mSocketUDP, struct sockaddr_in *clientAddress, int clientLengthUDP)
123+
{
124+
info("SENDFILE", "sending file...");
125+
FILE *in_file = fopen("../assets/mock.pdf", "rb"); // read only
126+
127+
if (!in_file) // equivalent to saying if ( in_file == NULL )
128+
{
129+
printf("oops, file can't be read\n");
130+
exit(-1);
131+
}
132+
fseek(in_file, 0L, SEEK_END);
133+
long size = ftell(in_file) / 8;
134+
rewind(in_file);
135+
136+
unsigned char buffer[1024]; // to be sent bufffer
137+
unsigned char fileBuffer[size]; // contains the whole file
138+
bzero(buffer, sizeof(buffer)); //set to zero
139+
bzero(fileBuffer, sizeof(fileBuffer)); //set to zero
140+
141+
fread(fileBuffer, sizeof(fileBuffer), 1, in_file);
142+
143+
// segment
144+
long nbOfSegment = size / 1020;
145+
for (int i = 0; i < nbOfSegment; i++)
146+
{
147+
buffer[0] = (char)i / 100;
148+
buffer[1] = (char)(i / 100) % 10;
149+
buffer[2] = (char)i % 10;
150+
// buffer[3]="-";
151+
for (int j = 0; j < 1020; j++)
152+
{
153+
buffer[j + 3] = fileBuffer[i * 1020 + j];
154+
info("SENDFILE", "file segmented...");
155+
156+
if (sendto(mSocketUDP, buffer, sizeof(buffer), 0, clientAddress, clientLengthUDP) > 0)
157+
{
158+
info("SENDFILE", "segment sent 🎉...");
159+
}
160+
else
161+
{
162+
perror("[CONNECT] SYNACK couldn't be sent...\n");
163+
return 0;
164+
}
165+
}
166+
}
167+
}

src/server/server.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#include <string.h>
22

3-
int handshake(int mSocketUDP, struct sockaddr_in *clientAddress, int clientLengthUDP);
3+
int handshake(int mSocketUDP, struct sockaddr_in *clientAddress, int clientLengthUDP);
4+
int sendFile();

0 commit comments

Comments
 (0)