54 lines
1.7 KiB
C
54 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h> //strlen
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h> //inet_addr
|
|
#include <unistd.h> //write
|
|
#include <netdb.h>
|
|
|
|
struct sockaddr_in sa;
|
|
int main()
|
|
{
|
|
int chunkSize;
|
|
int fileSize;
|
|
|
|
int my_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
sa.sin_family = AF_INET;
|
|
sa.sin_port = htons(10059);
|
|
inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr));
|
|
int x = connect(my_sock, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
// Request File Size
|
|
printf("\nRequesting File Size\n");
|
|
send(my_sock, "FS", 2, 0);
|
|
char size_buff[13];
|
|
int recv_size = recv(my_sock, size_buff, 13, 0);
|
|
fileSize = atoi(strtok(size_buff, "|"));
|
|
chunkSize = atoi(strtok(NULL, "|"));
|
|
|
|
// Server Transfer Settings
|
|
printf("File Size: %d\n", fileSize);
|
|
printf("Chunk Size: %d\n", chunkSize);
|
|
|
|
// Request File
|
|
printf("\nRequesting File Transfer\n");
|
|
send(my_sock, "RX", 2, 0);
|
|
|
|
int total_bytes_received = 0;
|
|
FILE *fp = fopen("rx.txt", "w");
|
|
char file_buffer[chunkSize];
|
|
while(total_bytes_received < fileSize) {
|
|
int bytes_received = recv(my_sock, file_buffer, chunkSize, 0);
|
|
if (bytes_received <= 0) {break;}
|
|
total_bytes_received += bytes_received;
|
|
fwrite(file_buffer, 1, bytes_received, fp);
|
|
printf("Received %d bytes, Total: %d bytes\n", bytes_received, total_bytes_received);
|
|
}
|
|
// Calculate MD5 checksum using system command
|
|
printf("\nCalculating MD5 checksum...\n");
|
|
printf("Expected Checksum:\n");
|
|
system("md5sum bits.txt | awk '{ print $1 }'");
|
|
printf("Received File Checksum: \n");
|
|
system("md5sum rx.txt | awk '{ print $1 }'");
|
|
fclose(fp);
|
|
} |