Skip to content

Commit

Permalink
resolved high ram usage when transferring large file
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailuReeves committed Dec 21, 2023
1 parent beb48fb commit 8958257
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
17 changes: 9 additions & 8 deletions WebShare-Connect/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// This is the header for the file
typedef struct {
int file_size;
int64_t file_size;
int chunk_size;
} file_header_t;

Expand Down Expand Up @@ -54,9 +54,9 @@ void client_receive(zsock_t* client_sock, const char* output_file_path) {
free(header_buf); // Free the received buffer after copying

printf("Received file header\n");
int file_size = header.file_size;
int64_t file_size = header.file_size;
int chunk_size = header.chunk_size;
printf("Expected file size: %d bytes\n", file_size);
printf("Expected file size: %lld bytes\n", file_size);
printf("Chunk size: %d bytes\n", chunk_size);

char* buffer = (char*)malloc(chunk_size);
Expand All @@ -77,23 +77,24 @@ void client_receive(zsock_t* client_sock, const char* output_file_path) {
int current_chunk = 0;

while (received_size < file_size) {
size_t size;
if (zsock_recv(client_sock, "b", &buffer, &size, file_size) == -1) { // Receive a chunk of data from the server socket and store it in the buffer variable (with a maximum size of chunk_size)
fprintf(stderr, "Error receiving data\n");
int size = zmq_recv(zsock_resolve(client_sock), buffer, chunk_size, 0);
if (size == -1) {
fprintf(stderr, "Error receiving data: %s\n", zmq_strerror(errno));
break;
}

size_t written = fwrite(buffer, 1, size, fp);
if (written < size) {
fprintf(stderr, "Error writing to file: expected %zu, wrote %zu\n", size, written);
fprintf(stderr, "Error writing to file: expected %d bytes, wrote %zu bytes\n", size, written);
break;
}
if (zstr_send(client_sock, "ACK") == -1) {
fprintf(stderr, "Error sending acknowledgment: %s\n", zmq_strerror(errno));
}
received_size += size;
current_chunk++;
printf("Received and wrote chunk %zu bytes\n", written);
fflush(&buffer);
//printf("Received chunk %d/%d with size %zu bytes\n", current_chunk, chunk_count, size);
}

free(buffer);
Expand Down
17 changes: 8 additions & 9 deletions WebShare-Connect/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// This is the header for the file
typedef struct {
int file_size;
int64_t file_size;
int chunk_size;
} file_header_t;

Expand Down Expand Up @@ -37,7 +37,7 @@ void server_send(zsock_t* serv_sock, const char* file_path)

// Determine file size
fseek(fp, 0L, SEEK_END);
int file_size = ftell(fp);
int64_t file_size = _ftelli64(fp);
rewind(fp);
if (file_size < 1)
{
Expand All @@ -46,7 +46,7 @@ void server_send(zsock_t* serv_sock, const char* file_path)
return;
}

printf("File size: %d bytes\n", file_size);
printf("File size: %lld bytes\n", file_size);

int chunk_size;

Expand Down Expand Up @@ -120,7 +120,11 @@ void server_send(zsock_t* serv_sock, const char* file_path)
zsock_send(serv_sock, "b", buffer, bytesRead); // Send file chunk

current_chunk++;
printf("Sent chunk %d/%d with size %lld bytes\n", current_chunk, chunk_count, bytesRead);
if (current_chunk % 100 == 0)
{
printf("Sent chunk %d/%d with size %lld bytes\n", current_chunk, chunk_count, bytesRead);
}


char* ack = zstr_recv(serv_sock);
if (!ack) {
Expand Down Expand Up @@ -156,11 +160,6 @@ int server_main(int argc, char const* argv[]) {
zsock_t* serv_sock = server(context, port, threads);
assert(serv_sock);

// Wait for client to connect
// Note: In PUSH/PULL, PUSH socket can send messages even if no clients are connected,
// messages will be queued until clients connect.
// Add logic to wait for client connection if needed.

// Send the file
server_send(serv_sock, file_path);

Expand Down

0 comments on commit 8958257

Please sign in to comment.