Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ 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/
#.idea/

# Added by cargo

/target
received_file
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rust_share" # Modifica il nome del crate in snake case
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
2 changes: 2 additions & 0 deletions received/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# RustShare
A system to share files between devices in the same network
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const PORT: u16 = 7878;
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::env;
use std::io;
mod sender;
mod receiver;
mod common;

fn print_usage() {
println!("Usage:");
println!(" send <address> <file_path> - Send a file to the specified address");
println!(" receive - Receive files on the specified port");
}

fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() == 4 && args[1] == "send" {
sender::send_file(&args[2], &args[3])?;
} else {
receiver::receive_file()?;
}
Ok(())
}
47 changes: 47 additions & 0 deletions src/receiver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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!("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) = reader.read(&mut buffer) {
if n == 0 {
break;
}
file.write_all(&buffer[..n])?;
}
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!("Listening on port {}", PORT);

for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(move || {
if let Err(e) = handle_client(stream) {
eprintln!("Error handling client: {}", e);
}
});
},
Err(e) => eprintln!("Connection error: {}", e),
}
}
Ok(())
}
28 changes: 28 additions & 0 deletions src/sender.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;
}
stream.write_all(&buffer[..n])?;
}

stream.shutdown(Shutdown::Write)?;
println!("File successfully sent to {}", addr);
Ok(())
}
Loading