From 919e63d5bf860303cf7695624e433a6afd85296d Mon Sep 17 00:00:00 2001 From: Alessio Pascucci Date: Sat, 15 Feb 2025 19:39:46 +0100 Subject: [PATCH 1/5] Cargo init Signed-off-by: Alessio Pascucci --- .gitignore | 6 +++++- Cargo.toml | 8 ++++++++ src/main.rs | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index d01bd1a..efe3eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,8 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7bdd2f9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "RustShare" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From a6553a9bc1b1159d2e44d4d4fe7d14e00525e807 Mon Sep 17 00:00:00 2001 From: Alessio Pascucci Date: Sat, 15 Feb 2025 19:51:05 +0100 Subject: [PATCH 2/5] Simple sender and receiver Write a simple sender and receiver feature. When the program is launched without arguments, it waits for a connection to receive file. Providing the "send" argument instead, and specifying a destination and the path for a file to send, the file is sent to another instance of the same program. Signed-off-by: Alessio Pascucci --- .gitignore | 1 + src/common.rs | 1 + src/main.rs | 16 ++++++++++++++-- src/receiver.rs | 36 ++++++++++++++++++++++++++++++++++++ src/sender.rs | 18 ++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/common.rs create mode 100644 src/receiver.rs create mode 100644 src/sender.rs diff --git a/.gitignore b/.gitignore index efe3eb1..78192d7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ Cargo.lock # Added by cargo /target +received_file diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..d3f34f1 --- /dev/null +++ b/src/common.rs @@ -0,0 +1 @@ +pub const PORT: u16 = 7878; diff --git a/src/main.rs b/src/main.rs index e7a11a9..5e08ff7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,15 @@ -fn main() { - println!("Hello, world!"); +use std::env; +use std::io; +mod sender; +mod receiver; +mod common; + +fn main() -> io::Result<()> { + let args: Vec = env::args().collect(); + if args.len() == 4 && args[1] == "send" { + sender::send_file(&args[2], &args[3])?; + } else { + receiver::receive_file()?; + } + Ok(()) } diff --git a/src/receiver.rs b/src/receiver.rs new file mode 100644 index 0000000..bf80b11 --- /dev/null +++ b/src/receiver.rs @@ -0,0 +1,36 @@ +use std::{fs::File, io::{self, Read, Write}, net::{TcpListener, TcpStream}, thread}; +use crate::common::PORT; + +fn handle_client(mut stream: TcpStream) -> io::Result<()> { + println!("Connessione ricevuta"); + let mut file = File::create("received_file")?; + let mut buffer = [0; 1024]; + + while let Ok(n) = stream.read(&mut buffer) { + if n == 0 { + break; + } + file.write_all(&buffer[..n])?; + } + println!("File ricevuto e salvato come 'received_file'"); + Ok(()) +} + +pub fn receive_file() -> io::Result<()> { + let listener = TcpListener::bind(format!("0.0.0.0:{}", PORT))?; + println!("In ascolto sulla porta {}", PORT); + + for stream in listener.incoming() { + match stream { + Ok(stream) => { + thread::spawn(move || { + if let Err(e) = handle_client(stream) { + eprintln!("Errore nel gestire il client: {}", e); + } + }); + }, + Err(e) => eprintln!("Errore durante la connessione: {}", e), + } + } + Ok(()) +} diff --git a/src/sender.rs b/src/sender.rs new file mode 100644 index 0000000..1511eb4 --- /dev/null +++ b/src/sender.rs @@ -0,0 +1,18 @@ +use std::{fs::File, io::{self, Read, Write}, net::{TcpStream, Shutdown}}; + +pub fn send_file(addr: &str, file_path: &str) -> io::Result<()> { + let mut stream = TcpStream::connect(addr)?; + let mut file = File::open(file_path)?; + let mut buffer = [0; 1024]; + + while let Ok(n) = file.read(&mut buffer) { + if n == 0 { + break; + } + stream.write_all(&buffer[..n])?; + } + + stream.shutdown(Shutdown::Write)?; + println!("File inviato con successo a {}", addr); + Ok(()) +} From 1f8892ad46d00c73013d0352eb04856f7107a97a Mon Sep 17 00:00:00 2001 From: Alessio Pascucci Date: Sat, 15 Feb 2025 20:08:17 +0100 Subject: [PATCH 3/5] Specify filename when sending and save under received directory Signed-off-by: Alessio Pascucci --- received/README.md | 2 ++ src/receiver.rs | 27 +++++++++++++++++++-------- src/sender.rs | 14 ++++++++++++-- 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 received/README.md diff --git a/received/README.md b/received/README.md new file mode 100644 index 0000000..ae015aa --- /dev/null +++ b/received/README.md @@ -0,0 +1,2 @@ +# RustShare +A system to share files between devices in the same network diff --git a/src/receiver.rs b/src/receiver.rs index bf80b11..3ada5ac 100644 --- a/src/receiver.rs +++ b/src/receiver.rs @@ -1,35 +1,46 @@ -use std::{fs::File, io::{self, Read, Write}, net::{TcpListener, TcpStream}, thread}; +use std::{fs::{self, File}, io::{self, BufRead, BufReader, Read, Write}, net::{TcpListener, TcpStream}, thread}; use crate::common::PORT; fn handle_client(mut stream: TcpStream) -> io::Result<()> { - println!("Connessione ricevuta"); - let mut file = File::create("received_file")?; + println!("Connection received"); + + // Read the file name + let mut reader = BufReader::new(&mut stream); + let mut file_name = String::new(); + reader.read_line(&mut file_name)?; + let file_name = file_name.trim(); + + // Create the "received" directory if it doesn't exist + fs::create_dir_all("received")?; + + let file_path = format!("received/{}", file_name); + let mut file = File::create(file_path)?; let mut buffer = [0; 1024]; - while let Ok(n) = stream.read(&mut buffer) { + while let Ok(n) = reader.read(&mut buffer) { if n == 0 { break; } file.write_all(&buffer[..n])?; } - println!("File ricevuto e salvato come 'received_file'"); + println!("File received and saved as 'received/{}'", file_name); Ok(()) } pub fn receive_file() -> io::Result<()> { let listener = TcpListener::bind(format!("0.0.0.0:{}", PORT))?; - println!("In ascolto sulla porta {}", PORT); + println!("Listening on port {}", PORT); for stream in listener.incoming() { match stream { Ok(stream) => { thread::spawn(move || { if let Err(e) = handle_client(stream) { - eprintln!("Errore nel gestire il client: {}", e); + eprintln!("Error handling client: {}", e); } }); }, - Err(e) => eprintln!("Errore durante la connessione: {}", e), + Err(e) => eprintln!("Connection error: {}", e), } } Ok(()) diff --git a/src/sender.rs b/src/sender.rs index 1511eb4..112de9c 100644 --- a/src/sender.rs +++ b/src/sender.rs @@ -1,10 +1,20 @@ -use std::{fs::File, io::{self, Read, Write}, net::{TcpStream, Shutdown}}; +use std::{fs::File, io::{self, Read, Write}, net::{TcpStream, Shutdown}, path::Path}; pub fn send_file(addr: &str, file_path: &str) -> io::Result<()> { let mut stream = TcpStream::connect(addr)?; let mut file = File::open(file_path)?; let mut buffer = [0; 1024]; + // Get the file name from the file_path + let file_name = Path::new(file_path) + .file_name() + .and_then(|name| name.to_str()) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid file name"))?; + + // Send the file name + stream.write_all(file_name.as_bytes())?; + stream.write_all(b"\n")?; + while let Ok(n) = file.read(&mut buffer) { if n == 0 { break; @@ -13,6 +23,6 @@ pub fn send_file(addr: &str, file_path: &str) -> io::Result<()> { } stream.shutdown(Shutdown::Write)?; - println!("File inviato con successo a {}", addr); + println!("File successfully sent to {}", addr); Ok(()) } From e7803c04e143e9fc7050886bf7a111e2bce3c678 Mon Sep 17 00:00:00 2001 From: Alessio Pascucci Date: Sat, 15 Feb 2025 20:17:23 +0100 Subject: [PATCH 4/5] Add helper Signed-off-by: Alessio Pascucci --- src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5e08ff7..18cca01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,12 @@ mod sender; mod receiver; mod common; +fn print_usage() { + println!("Usage:"); + println!(" send
- Send a file to the specified address"); + println!(" receive - Receive files on the specified port"); +} + fn main() -> io::Result<()> { let args: Vec = env::args().collect(); if args.len() == 4 && args[1] == "send" { From 854637787fe9b539ceba962f8f56a10d18ddaae6 Mon Sep 17 00:00:00 2001 From: Alessio Pascucci Date: Sat, 15 Feb 2025 20:17:50 +0100 Subject: [PATCH 5/5] refactor: rename crate using snake case Signed-off-by: Alessio Pascucci --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7bdd2f9..412079d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "RustShare" +name = "rust_share" # Modifica il nome del crate in snake case version = "0.1.0" edition = "2021"