Final
This commit is contained in:
Binary file not shown.
@@ -9,9 +9,10 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.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 SERVER_PORT 60019
|
||||||
#define METHOD_VERSION 1
|
#define METHOD_VERSION 2
|
||||||
|
|
||||||
#define HEADER_SIZE 4
|
#define HEADER_SIZE 4
|
||||||
#define CHUNK_SIZE 1024
|
#define CHUNK_SIZE 1024
|
||||||
@@ -51,7 +52,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// Set socket timeout to 5 seconds
|
// Set socket timeout to 5 seconds
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
timeout.tv_sec = 10;
|
timeout.tv_sec = 5;
|
||||||
timeout.tv_usec = 0;
|
timeout.tv_usec = 0;
|
||||||
setsockopt(socket_desc, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
setsockopt(socket_desc, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ listen_for_file:
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
buffer[recv_size] = '\0';
|
buffer[recv_size] = '\0';
|
||||||
char chunkNum[5];
|
char chunkNum[HEADER_SIZE];
|
||||||
strncpy(chunkNum, buffer, HEADER_SIZE);
|
strncpy(chunkNum, buffer, HEADER_SIZE);
|
||||||
chunkNum[HEADER_SIZE] = '\0';
|
chunkNum[HEADER_SIZE] = '\0';
|
||||||
ack_buffer[atoi(chunkNum)] = 1; // Set buffer to RXed
|
ack_buffer[atoi(chunkNum)] = 1; // Set buffer to RXed
|
||||||
@@ -103,8 +104,7 @@ listen_for_file:
|
|||||||
if (totalMissing != 0)
|
if (totalMissing != 0)
|
||||||
{
|
{
|
||||||
printf("Total missing chunks: %d, requesting retransmission...\n", totalMissing);
|
printf("Total missing chunks: %d, requesting retransmission...\n", totalMissing);
|
||||||
char request[TOTAL_CHUNKS + HEADER_SIZE];
|
char request[TOTAL_CHUNKS + HEADER_SIZE] = "REQ";
|
||||||
snprintf(request, sizeof(request), "REQ");
|
|
||||||
memcpy(request + HEADER_SIZE, ack_buffer, TOTAL_CHUNKS);
|
memcpy(request + HEADER_SIZE, ack_buffer, TOTAL_CHUNKS);
|
||||||
sendto(socket_desc, request, strlen(request), 0, (struct sockaddr *)&server, sizeof(server));
|
sendto(socket_desc, request, strlen(request), 0, (struct sockaddr *)&server, sizeof(server));
|
||||||
goto listen_for_file;
|
goto listen_for_file;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <netdb.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
#define SERVER_PORT 60019
|
#define SERVER_PORT 60019
|
||||||
|
|
||||||
@@ -40,11 +40,6 @@ int main(int argc, char *argv[])
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
recv_size = recvfrom(socket_desc, client_message, HEADER_SIZE + AWK_BUFFER_SIZE, 0, (struct sockaddr *)&client, (socklen_t *)&c);
|
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';
|
client_message[recv_size] = '\0';
|
||||||
|
|
||||||
@@ -54,18 +49,17 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
char buffer[CHUNK_SIZE];
|
char buffer[CHUNK_SIZE];
|
||||||
int chunk_number = 1;
|
int chunk_number = 1;
|
||||||
size_t bytes_read;
|
|
||||||
|
|
||||||
fseek(file, 0, SEEK_SET); // Reset file pointer to beginning
|
fseek(file, 0, SEEK_SET); // Reset file pointer to beginning
|
||||||
for (int i = 0; i < TOTAL_CHUNKS; i++)
|
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];
|
char chunk_response[HEADER_SIZE + CHUNK_SIZE];
|
||||||
snprintf(chunk_response, HEADER_SIZE + 1, "%04d", chunk_number);
|
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++;
|
chunk_number++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user