Skip to content

Commit

Permalink
Merge pull request #6 from Vansh7388/main
Browse files Browse the repository at this point in the history
solved document error
  • Loading branch information
rajpatel8 authored May 20, 2024
2 parents 1514e7a + d37b244 commit 11b3aed
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 311 deletions.
272 changes: 144 additions & 128 deletions client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <fstream>
#include <sys/stat.h>

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

using namespace std;

Expand All @@ -31,174 +31,190 @@ class Client {
int client_socket;
struct sockaddr_in server_addr;

public:
Client(const string& ip_address, int port) {
create_socket();
setup_server_address(ip_address, port);
connect_to_server();
public:
Client(const string& ip_address, int port) {
create_socket();
setup_server_address(ip_address, port);
connect_to_server();
}

~Client() {
close(client_socket);
}

void run() {
authenticate();
send_files_in_directory(DIRECTORY_PATH);
}

private:
void create_socket() {
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
}

~Client() {
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);
}
}

void run() {
authenticate();
send_files_in_directory(DIRECTORY_PATH);
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);
}
}

private:
void create_socket() {
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
void authenticate() {
char token[1024];
if (recv(client_socket, token, sizeof(token), 0) == -1) {
perror("Error receiving token from server");
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(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");
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");
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");
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;
bool send_file(const string& filepath) {
ifstream file(filepath, ios::binary);
if (!file) {
perror("Error opening file");
return false;
}

char signal[1024] = {0};
if (recv(client_socket, signal, sizeof(signal), 0) == -1) {
perror("Error receiving start signal from server");
exit(EXIT_FAILURE);
}
// Get the file size
file.seekg(0, ios::end);
size_t filesize = file.tellg();
file.seekg(0, ios::beg);

cout << "Server signal: " << signal << endl;
// Send the filename
string filename = filepath.substr(filepath.find_last_of("/") + 1);
if (filename.empty()) {
cerr << "Error: Empty filename, skipping file: " << filepath << endl;
return false;
}

bool send_file(const string& filepath) {
ifstream file(filepath, ios::binary);
if (!file) {
perror("Error opening file");
return false;
}
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;
}

// Get the file size
file.seekg(0, ios::end);
size_t filesize = file.tellg();
file.seekg(0, ios::beg);
// 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 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");
// 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;
}

// Send the file size
if (send(client_socket, &filesize, sizeof(filesize), 0) == -1) {
perror("Error sending file size to server");
}
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;
}
}

// 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;
}

file.close();
return true;
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");
send_end_signal();
exit(EXIT_FAILURE);
}

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);
bool files_sent = false;
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}

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;
string filepath = directory_path + string(ent->d_name);
if (!send_file(filepath)) {
closedir(dir);
send_end_signal();
exit(EXIT_FAILURE);
}

// 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");
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);
send_end_signal();
exit(EXIT_FAILURE);
}
cout << "\nAll files sent to the server\n";
cout << "Server confirmation: " << confirmation << endl;
files_sent = true;
}

closedir(dir);
if (!files_sent) {
cout << "No files to send.\n";
}

closedir(dir);
send_end_signal();
cout << "\nAll files sent to the server\n";
}

void send_end_signal() {
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);
}
}
};

int main() {
Expand Down
Loading

0 comments on commit 11b3aed

Please sign in to comment.