final
This commit is contained in:
Binary file not shown.
@@ -47,8 +47,8 @@ int main()
|
||||
// Calculate MD5 checksum using system command
|
||||
printf("\nCalculating MD5 checksum...\n");
|
||||
printf("Expected Checksum:\n");
|
||||
// system("md5sum bits.txt | awk '{ print $1 }'");
|
||||
system("md5sum bits.txt | awk '{ print $1 }'");
|
||||
printf("Received File Checksum: \n");
|
||||
// system("md5sum rx.txt | awk '{ print $1 }'");
|
||||
system("md5sum rx.txt | awk '{ print $1 }'");
|
||||
fclose(fp);
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -1,56 +0,0 @@
|
||||
#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>
|
||||
|
||||
#define EXPECTED_CHECKSUM "9f1b7e4717bffb60d6d6c39b427932e1"
|
||||
|
||||
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));
|
||||
perror("Connect");
|
||||
|
||||
// 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;
|
||||
if (access("rx.txt", F_OK) == 0) {remove("rx.txt");}
|
||||
FILE *fp = fopen("rx.txt", "a");
|
||||
while(total_bytes_received < fileSize) {
|
||||
char file_buffer[chunkSize];
|
||||
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%s\nReceived File Checksum: \n", EXPECTED_CHECKSUM);
|
||||
system("md5sum rx.txt | awk '{ print $1 }'");
|
||||
fclose(fp);
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
|
||||
|
||||
#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 CHUNK_SIZE 8192
|
||||
|
||||
int send_size, recv_size;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int socket_desc, client_sock, c, read_size; // sockets and such
|
||||
struct sockaddr_in server, client; // one for listening one for connection with client(s)
|
||||
char client_message[CHUNK_SIZE]; // Maximum size of the message buffer
|
||||
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
|
||||
client_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
printf("Sockets Created\n");
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(10059);
|
||||
inet_pton(AF_INET, "127.0.0.1", &(server.sin_addr));
|
||||
|
||||
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
|
||||
{
|
||||
perror("Bind Failed"); // perror is a very helpful function for tracking down errors
|
||||
return 1;
|
||||
}
|
||||
printf("Bind completed \n");
|
||||
|
||||
c = sizeof(struct sockaddr_in);
|
||||
|
||||
listen(socket_desc, 1);
|
||||
|
||||
printf("Waiting for incoming connections...\n");
|
||||
|
||||
// Start waiting for any connections inbound
|
||||
while (1)
|
||||
{
|
||||
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
|
||||
printf("\nClient Connected\n");
|
||||
while (1)
|
||||
{
|
||||
recv_size = recv(client_sock, client_message, 2, 0);
|
||||
long fileSize;
|
||||
if (recv_size == 0)
|
||||
{
|
||||
printf("Client disconnected\n");
|
||||
break;
|
||||
}
|
||||
client_message[recv_size] = '\0';
|
||||
printf("Received %d bytes. Msg is %s \n", recv_size, client_message);
|
||||
// Determine type of message
|
||||
if (strncmp(client_message, "FS", 2) == 0)
|
||||
{
|
||||
// Send size of the file
|
||||
FILE *fp = fopen("bits.txt", "rb");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSize = ftell(fp);
|
||||
fclose(fp);
|
||||
sprintf(client_message, "%ld|%d", fileSize, CHUNK_SIZE);
|
||||
send_size = send(client_sock, client_message, strlen(client_message), 0);
|
||||
}
|
||||
else if (strncmp(client_message, "RX", 2) == 0)
|
||||
{
|
||||
// Client ready to receive file
|
||||
FILE *fp = fopen("bits.txt", "rb");
|
||||
for(int i = 0; i < fileSize; i += CHUNK_SIZE) {
|
||||
char buffer[CHUNK_SIZE];
|
||||
size_t bytesRead = fread(buffer, 1, CHUNK_SIZE, fp);
|
||||
if (bytesRead > 0) {
|
||||
send_size = send(client_sock, buffer, bytesRead, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
close(socket_desc);
|
||||
close(client_sock);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user