This commit is contained in:
2025-11-12 23:06:49 +00:00
parent f94572b1e5
commit d8f1aadf28
3 changed files with 13 additions and 19 deletions
Binary file not shown.
+6 -6
View File
@@ -9,9 +9,10 @@
#include <sys/time.h>
#include <unistd.h>
#define SERVER_IP_ADDRESS "149.165.171.99"
// #define SERVER_IP_ADDRESS "149.165.171.99"
#define SERVER_IP_ADDRESS "127.0.0.1"
#define SERVER_PORT 60019
#define METHOD_VERSION 1
#define METHOD_VERSION 2
#define HEADER_SIZE 4
#define CHUNK_SIZE 1024
@@ -51,7 +52,7 @@ int main(int argc, char *argv[])
// Set socket timeout to 5 seconds
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(socket_desc, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
@@ -72,7 +73,7 @@ listen_for_file:
else
{
buffer[recv_size] = '\0';
char chunkNum[5];
char chunkNum[HEADER_SIZE];
strncpy(chunkNum, buffer, HEADER_SIZE);
chunkNum[HEADER_SIZE] = '\0';
ack_buffer[atoi(chunkNum)] = 1; // Set buffer to RXed
@@ -103,8 +104,7 @@ listen_for_file:
if (totalMissing != 0)
{
printf("Total missing chunks: %d, requesting retransmission...\n", totalMissing);
char request[TOTAL_CHUNKS + HEADER_SIZE];
snprintf(request, sizeof(request), "REQ");
char request[TOTAL_CHUNKS + HEADER_SIZE] = "REQ";
memcpy(request + HEADER_SIZE, ack_buffer, TOTAL_CHUNKS);
sendto(socket_desc, request, strlen(request), 0, (struct sockaddr *)&server, sizeof(server));
goto listen_for_file;
+7 -13
View File
@@ -1,11 +1,11 @@
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#define SERVER_PORT 60019
@@ -40,11 +40,6 @@ int main(int argc, char *argv[])
while (1)
{
recv_size = recvfrom(socket_desc, client_message, HEADER_SIZE + AWK_BUFFER_SIZE, 0, (struct sockaddr *)&client, (socklen_t *)&c);
if (recv_size < 0)
{
perror("Receive Failed");
continue;
}
client_message[recv_size] = '\0';
@@ -54,18 +49,17 @@ int main(int argc, char *argv[])
char buffer[CHUNK_SIZE];
int chunk_number = 1;
size_t bytes_read;
fseek(file, 0, SEEK_SET); // Reset file pointer to beginning
for (int i = 0; i < TOTAL_CHUNKS; i++)
{
bytes_read = fread(buffer, 1, CHUNK_SIZE, file);
fread(buffer, 1, CHUNK_SIZE, file);
char chunk_response[HEADER_SIZE + CHUNK_SIZE];
snprintf(chunk_response, HEADER_SIZE + 1, "%04d", chunk_number);
memcpy(chunk_response + HEADER_SIZE, buffer, bytes_read);
memcpy(chunk_response + HEADER_SIZE, buffer, CHUNK_SIZE);
send_size = sendto(socket_desc, chunk_response, HEADER_SIZE + bytes_read, 0, (struct sockaddr *)&client, c);
send_size = sendto(socket_desc, chunk_response, HEADER_SIZE + CHUNK_SIZE, 0, (struct sockaddr *)&client, c);
chunk_number++;
}
}