diff --git a/client.cpp b/client.cpp index da5c645..1b4d71b 100644 --- a/client.cpp +++ b/client.cpp @@ -6,10 +6,12 @@ #include // For inet_pton and other network functions #include #include +#include +#include +#include using namespace std; - // FNV-1a hash function size_t fnv1a_hash(const string& str) { const size_t FNV_offset_basis = 14695981039346656037ULL; @@ -23,6 +25,55 @@ 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; + } + + // 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; + } + } + + file.close(); + return true; +} + int main() { // Create a socket int client_socket = socket(AF_INET, SOCK_STREAM, 0); @@ -34,23 +85,20 @@ int main() { // Set up server address struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(21); + server_addr.sin_port = htons(6969); - // Convert and set server IP address - if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) { // Replace with server IP address + 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); } - // 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); } - // Receive token from the server char token[1024]; if (recv(client_socket, token, sizeof(token), 0) == -1) { perror("Error receiving token from server"); @@ -60,12 +108,10 @@ int main() { cout << "\nToken received from the server: " << token << endl; - // Generate hash of the token string token_string = string(token); string token_hash = to_string(fnv1a_hash(token_string)); - // Send the hash to the server - if (send(client_socket, token_hash.c_str(), token_hash.size(), 0) == -1) { + 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); @@ -74,7 +120,6 @@ int main() { cout << "\nHash sent to the server\n"; cout << "\nWaiting for the server to authenticate...\n"; - // Receive authentication status from the server char auth_status[1024] = {0}; if (recv(client_socket, auth_status, sizeof(auth_status), 0) == -1) { perror("Error receiving authentication status from server"); @@ -84,7 +129,6 @@ int main() { cout << auth_status << endl; - // Wait for server to send start signal char signal[1024] = {0}; if (recv(client_socket, signal, sizeof(signal), 0) == -1) { perror("Error receiving start signal from server"); @@ -94,11 +138,51 @@ int main() { cout << "Server signal: " << signal << endl; - // Logic to send files goes here - // ... + DIR* dir; + struct dirent* ent; + if ((dir = opendir("test_data")) == NULL) { + perror("Error opening directory"); + close(client_socket); + exit(EXIT_FAILURE); + } + + while ((ent = readdir(dir)) != NULL) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + + string filepath = "test_data/" + string(ent->d_name); + if (!send_file(client_socket, filepath)) { + closedir(dir); + close(client_socket); + 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); + close(client_socket); + exit(EXIT_FAILURE); + } + cout << "Server confirmation: " << confirmation << endl; + } - // Close client socket + // 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); return 0; } + diff --git a/server.cpp b/server.cpp index 9f8da93..30b1127 100644 --- a/server.cpp +++ b/server.cpp @@ -10,6 +10,8 @@ #include #include // For mkdir #include +#include +#include using namespace std; @@ -82,9 +84,9 @@ bool createFolder(const string& folderName) { void handleClient(int client_socket) { // Send random string to client - string Token = generateRandomString(256); - cout << "Token: " << Token << '\n'; - if (send(client_socket, Token.c_str(), Token.size(), 0) == -1) { + string token = generateRandomString(255); + cout << "Token: " << token << '\n'; + if (send(client_socket, token.c_str(), token.size() + 1, 0) == -1) { // include null terminator perror("Error sending token to client"); close(client_socket); return; @@ -92,7 +94,7 @@ void handleClient(int client_socket) { cout << "Token sent to client\nWaiting for client to send hash\n"; // Hash the token and wait for the client to send the hash - string expectedHash = to_string(fnv1a_hash(Token)); + string expectedHash = to_string(fnv1a_hash(token)); char receivedHash[1024] = {0}; if (recv(client_socket, receivedHash, sizeof(receivedHash), 0) == -1) { perror("Error receiving hash from client"); @@ -102,10 +104,14 @@ void handleClient(int client_socket) { cout << "Hash received from client\nChecking hash...\n"; string receivedHashString = string(receivedHash); + + cout << "Received hash: " << receivedHashString << endl; + cout << "Expected hash: " << expectedHash << endl; + if (authenticateClient(receivedHashString, expectedHash)) { cout << "Hash verified!\n"; string successMessage = "Authentication successful\n"; - if (send(client_socket, successMessage.c_str(), successMessage.size(), 0) == -1) { + if (send(client_socket, successMessage.c_str(), successMessage.size() + 1, 0) == -1) { // include null terminator perror("Error sending success message to client"); close(client_socket); return; @@ -124,9 +130,67 @@ void handleClient(int client_socket) { } string signal = "start"; - if (send(client_socket, signal.c_str(), signal.size(), 0) == -1) { + if (send(client_socket, signal.c_str(), signal.size() + 1, 0) == -1) { // include null terminator perror("Error sending signal to client"); } + + // Loop to receive multiple files + while (true) { + // Receive file metadata + char filename[1024] = {0}; + if (recv(client_socket, filename, sizeof(filename), 0) == -1) { + perror("Error receiving filename from client"); + close(client_socket); + return; + } + if (strcmp(filename, "end_of_files") == 0) { + break; // End of file transmission + } + cout << "File name: " << filename << endl; + + // Receive file size + size_t filesize; + if (recv(client_socket, &filesize, sizeof(filesize), 0) == -1) { + perror("Error receiving file size from client"); + close(client_socket); + return; + } + cout << "File size: " << filesize << " bytes" << endl; + + string filepath = folderName + "/" + string(filename); + ofstream file(filepath, ios::binary); + if (!file) { + perror("Error opening file for writing"); + close(client_socket); + return; + } + + // Receive file data in chunks + char buffer[1024]; + size_t bytes_received = 0; + while (bytes_received < filesize) { + ssize_t n = recv(client_socket, buffer, sizeof(buffer), 0); + if (n == -1) { + perror("Error receiving file from client"); + close(client_socket); + return; + } + file.write(buffer, n); + bytes_received += n; + } + + cout << "File received and saved to " << filepath << endl; + + // Inform client that the file has been received + string fileReceived = "File received: "; + if (send(client_socket, fileReceived.c_str(), fileReceived.size() + 1, 0) == -1) { // include null terminator + perror("Error sending file received message to client"); + close(client_socket); + return; + } + } + + cout << "All files received.\n"; } else { cout << "Hash verification failed!\n"; } @@ -144,7 +208,7 @@ int main() { struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; - server_addr.sin_port = htons(21); + server_addr.sin_port = htons(6969); if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("Error binding socket"); @@ -153,12 +217,12 @@ int main() { } if (listen(server_socket, 3) == -1) { - perror("Error listening on port 21"); + perror("Error listening on port 6969"); close(server_socket); exit(EXIT_FAILURE); } - cout << "Listening on port 21\n"; + cout << "Listening on port 6969\n"; while (true) { int client_socket = accept(server_socket, NULL, NULL); @@ -173,4 +237,5 @@ int main() { close(server_socket); return 0; -} \ No newline at end of file +} +