Skip to content

Commit

Permalink
Implemented OOPS for better flow & trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord_Rajkumar committed May 19, 2024
1 parent c6a01c9 commit 5a45727
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 337 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ server
.vscode
rough.cpp
client
rough
rough
.DS_Store
296 changes: 158 additions & 138 deletions client.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib> // For exit()
#include <unistd.h> // For close()
#include <arpa/inet.h> // For inet_pton and other network functions
#include <cstdlib>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>

#define DIRECTORY_PATH "/Users/rajkumar/Desktop/Snap-Sync/tmp/"

using namespace std;

// FNV-1a hash function
Expand All @@ -25,164 +27,182 @@ size_t fnv1a_hash(const string& str) {
return hash;
}

// Function to send a file to the server
bool send_file(int socket, const string& filepath) {
ifstream file(filepath, ios::binary);
if (!file) {
perror("Error opening file");
return false;
}

// Get the file size
file.seekg(0, ios::end);
size_t filesize = file.tellg();
file.seekg(0, ios::beg);

// Send the filename
string filename = filepath.substr(filepath.find_last_of("/") + 1);
if (send(socket, filename.c_str(), filename.size() + 1, 0) == -1) { // include null terminator
perror("Error sending filename to server");
file.close();
return false;
}

// Send the file size
if (send(socket, &filesize, sizeof(filesize), 0) == -1) {
perror("Error sending file size to server");
file.close();
return false;
}
class Client {
int client_socket;
struct sockaddr_in server_addr;

// Send the file data
char buffer[1024];
while (file.read(buffer, sizeof(buffer))) {
if (send(socket, buffer, sizeof(buffer), 0) == -1) {
perror("Error sending file data to server");
file.close();
return false;
}
}
if (file.gcount() > 0) {
if (send(socket, buffer, file.gcount(), 0) == -1) {
perror("Error sending remaining file data to server");
file.close();
return false;
public:
Client(const string& ip_address, int port) {
create_socket();
setup_server_address(ip_address, port);
connect_to_server();
}
}

file.close();
return true;
}

int main() {
// Create a socket
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}

// Set up server address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(6969);
~Client() {
close(client_socket);
}

if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
close(client_socket);
exit(EXIT_FAILURE);
}
void run() {
authenticate();
send_files_in_directory(DIRECTORY_PATH);
}

if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
close(client_socket);
exit(EXIT_FAILURE);
}
private:
void create_socket() {
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
}

char token[1024];
if (recv(client_socket, token, sizeof(token), 0) == -1) {
perror("Error receiving token from server");
close(client_socket);
exit(EXIT_FAILURE);
}
void setup_server_address(const string& ip_address, int port) {
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip_address.c_str(), &server_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
close(client_socket);
exit(EXIT_FAILURE);
}
}

cout << "\nToken received from the server: " << token << endl;
void connect_to_server() {
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error connecting to server");
close(client_socket);
exit(EXIT_FAILURE);
}
}

string token_string = string(token);
string token_hash = to_string(fnv1a_hash(token_string));
void authenticate() {
char token[1024];
if (recv(client_socket, token, sizeof(token), 0) == -1) {
perror("Error receiving token from server");
exit(EXIT_FAILURE);
}

if (send(client_socket, token_hash.c_str(), token_hash.size() + 1, 0) == -1) { // include null terminator
perror("Error sending hash to server");
close(client_socket);
exit(EXIT_FAILURE);
}
cout << "\nToken received from the server: " << token << endl;

cout << "\nHash sent to the server\n";
cout << "\nWaiting for the server to authenticate...\n";
string token_string(token);
string token_hash = to_string(fnv1a_hash(token_string));

char auth_status[1024] = {0};
if (recv(client_socket, auth_status, sizeof(auth_status), 0) == -1) {
perror("Error receiving authentication status from server");
close(client_socket);
exit(EXIT_FAILURE);
}
if (send(client_socket, token_hash.c_str(), token_hash.size() + 1, 0) == -1) { // include null terminator
perror("Error sending hash to server");
exit(EXIT_FAILURE);
}

cout << auth_status << endl;
cout << "\nHash sent to the server\n";
cout << "\nWaiting for the server to authenticate...\n";

char signal[1024] = {0};
if (recv(client_socket, signal, sizeof(signal), 0) == -1) {
perror("Error receiving start signal from server");
close(client_socket);
exit(EXIT_FAILURE);
}
char auth_status[1024] = {0};
if (recv(client_socket, auth_status, sizeof(auth_status), 0) == -1) {
perror("Error receiving authentication status from server");
exit(EXIT_FAILURE);
}

cout << "Server signal: " << signal << endl;
cout << auth_status << endl;

DIR* dir;
struct dirent* ent;
if ((dir = opendir("test_data")) == NULL) {
perror("Error opening directory");
close(client_socket);
exit(EXIT_FAILURE);
}
char signal[1024] = {0};
if (recv(client_socket, signal, sizeof(signal), 0) == -1) {
perror("Error receiving start signal from server");
exit(EXIT_FAILURE);
}

while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
cout << "Server signal: " << signal << endl;
}

string filepath = "test_data/" + string(ent->d_name);
if (!send_file(client_socket, filepath)) {
closedir(dir);
close(client_socket);
exit(EXIT_FAILURE);
bool send_file(const string& filepath) {
ifstream file(filepath, ios::binary);
if (!file) {
perror("Error opening file");
return false;
}

// Get the file size
file.seekg(0, ios::end);
size_t filesize = file.tellg();
file.seekg(0, ios::beg);

// Send the filename
string filename = filepath.substr(filepath.find_last_of("/") + 1);
if (send(client_socket, filename.c_str(), filename.size() + 1, 0) == -1) { // include null terminator
perror("Error sending filename to server");
file.close();
return false;
}

// Send the file size
if (send(client_socket, &filesize, sizeof(filesize), 0) == -1) {
perror("Error sending file size to server");
file.close();
return false;
}

// Send the file data
char buffer[1024];
while (file.read(buffer, sizeof(buffer))) {
if (send(client_socket, buffer, sizeof(buffer), 0) == -1) {
perror("Error sending file data to server");
file.close();
return false;
}
}
if (file.gcount() > 0) {
if (send(client_socket, buffer, file.gcount(), 0) == -1) {
perror("Error sending remaining file data to server");
file.close();
return false;
}
}

file.close();
return true;
}

cout << "\nFile sent to the server: " << filepath << endl;
void send_files_in_directory(const string& directory_path) {
DIR* dir;
struct dirent* ent;
if ((dir = opendir(directory_path.c_str())) == NULL) {
perror("Error opening directory");
exit(EXIT_FAILURE);
}

while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}

string filepath = directory_path + string(ent->d_name);
if (!send_file(filepath)) {
closedir(dir);
exit(EXIT_FAILURE);
}

cout << "\nFile sent to the server: " << filepath << endl;

// Wait for confirmation from the server before sending the next file
char confirmation[1024];
if (recv(client_socket, confirmation, sizeof(confirmation), 0) == -1) {
perror("Error receiving confirmation from server");
closedir(dir);
exit(EXIT_FAILURE);
}
cout << "Server confirmation: " << confirmation << endl;
}

// Send end signal to indicate all files have been sent
string end_signal = "end_of_files";
if (send(client_socket, end_signal.c_str(), end_signal.size() + 1, 0) == -1) { // include null terminator
perror("Error sending end signal to server");
exit(EXIT_FAILURE);
}
cout << "\nAll files sent to the server\n";

// Wait for confirmation from the server before sending the next file
char confirmation[1024];
if (recv(client_socket, confirmation, sizeof(confirmation), 0) == -1) {
perror("Error receiving confirmation from server");
closedir(dir);
close(client_socket);
exit(EXIT_FAILURE);
}
cout << "Server confirmation: " << confirmation << endl;
}

// Send end signal to indicate all files have been sent
string end_signal = "end_of_files";
if (send(client_socket, end_signal.c_str(), end_signal.size() + 1, 0) == -1) { // include null terminator
perror("Error sending end signal to server");
close(client_socket);
exit(EXIT_FAILURE);
}
cout << "\nAll files sent to the server\n";

closedir(dir);
close(client_socket);
};

int main() {
Client client("127.0.0.1", 6969);
client.run();
return 0;
}

Loading

0 comments on commit 5a45727

Please sign in to comment.