rust-tcp-file-transfer/src/main.rs

208 lines
8 KiB
Rust
Raw Normal View History

2023-09-14 08:29:32 +03:00
use std::fs::File;
use std::net::{TcpListener, TcpStream};
2023-09-16 19:15:30 +03:00
use std::io::{Read, Write, self};
use std::env::{self};
2023-09-14 08:29:32 +03:00
2023-09-16 19:15:30 +03:00
struct FileInfo
2023-09-14 08:29:32 +03:00
{
2023-09-16 19:15:30 +03:00
file:Option<File>,
location:String,
bytes:Vec<u8>,
size:usize,
2023-09-14 08:29:32 +03:00
}
2023-09-16 19:15:30 +03:00
impl FileInfo
2023-09-14 08:29:32 +03:00
{
2023-09-16 19:15:30 +03:00
fn read_file(&mut self)
{
self.file = Some(File::open(&self.location).expect("Error: Open File"))
}
fn file_to_byte(&mut self)
{
match self.file
{
Some(ref mut file_descriptor) =>
{
match File::read_to_end(file_descriptor, &mut self.bytes)
{
Ok(size) =>
{
self.size = size;
println!("Done: Read -> {} bytes", size);
}
Err(err_val) => println!("Error: Read {}", err_val),
}
}
None =>
{
println!("Error: File None");
}
}
}
fn write_file(&mut self)
2023-09-14 08:29:32 +03:00
{
2023-09-16 19:15:30 +03:00
self.file = Some(File::create(&self.location).expect("Error: Create File"));
match self.file
{
Some(ref mut file_descriptor) =>
{
match File::write_all(file_descriptor, &self.bytes)
{
Ok(_) => println!("Done:Writing"),
Err(err_val) => println!("Failed:Writing {}", err_val),
}
}
None =>
{
println!("Error: File None");
}
}
2023-09-14 08:29:32 +03:00
}
}
2023-09-16 19:15:30 +03:00
enum Connection
{
Server(String, String),
Client(String, String),
}
impl Connection
2023-09-14 08:29:32 +03:00
{
2023-09-16 19:15:30 +03:00
fn server(self, file_info:&mut FileInfo)
{
print!("Server: ");
let ip:String;
let port:String;
let address:String;
match self
{
Connection::Server(in1, in2) =>
{
ip = in1.trim_end().to_string();
port = in2.trim_end().to_string();
address = format!("{}:{}", ip, port);
println!("{}", address);
}
_ => return
}
let socket = TcpListener::bind(address);
for stream in socket.expect("Error: Can't Check Connections").incoming()
{
match stream
{
Ok(mut stream) =>
{
let stay = true;
while stay
{
let mut data = vec![];
match stream.read(&mut data)
{
Ok(res) =>
{
if res == 0
{
println!("Connection Closed");
return;
}
FileInfo::write_file(file_info)
}
Err(e) =>
{
println!("Error: Failed to Read -> {}", e);
return;
}
}
}
}
Err(e) =>
{
println!("Error: Can't Visit Stream -> {}", e);
return;
}
}
}
}
fn client(self, file_info:&mut FileInfo)
2023-09-14 08:29:32 +03:00
{
2023-09-16 19:15:30 +03:00
print!("Client: ");
let ip:String;
let port:String;
let address:String;
match self
{
Connection::Client(in1, in2) =>
{
ip = in1.trim_end().to_string();
port = in2.trim_end().to_string();
address = format!("{}:{}", ip, port);
println!("{}", address);
}
_ => return
}
match TcpStream::connect(address)
{
Ok(mut socket) =>
{
println!("Connected");
FileInfo::read_file(file_info);
FileInfo::file_to_byte(file_info);
socket.write(&file_info.bytes).unwrap();
}
Err(e) =>
{
println!("Error: Connection -> {}", e);
}
}
2023-09-14 08:29:32 +03:00
}
}
2023-09-16 19:15:30 +03:00
fn take_string(output:String) -> String
{
let mut input = String::new();
println!("{}", output);
io::stdin().read_line(&mut input).expect("Failed to Read from Console");
input
}
fn take_arg() -> String
{
env::args().last().as_deref().unwrap_or("default").to_string()
}
2023-09-14 08:29:32 +03:00
fn main()
{
println!("Hello, world!");
2023-09-16 19:15:30 +03:00
let bytes:Vec<u8> = vec![];
let size:usize = 0 as usize;
let mut data = FileInfo
{
file:None,
location:take_arg(),
bytes:bytes,
size:size,
};
match &take_string("Input: Server 's', Client 'c'".to_string())[0..1]
{
"s" =>
{
Connection::server
(Connection::Server(take_string("Input: Server Stream IP Address".to_string()),
take_string("Input: Server Stream Port Address".to_string())),
&mut data);
},
"c" =>
{
Connection::client
(Connection::Client(take_string("Input: Server IP Address to Connect".to_string()),
take_string("Input: Server Port Address to Connect".to_string())),
&mut data);
}
input =>
{
println!("Error: Give Valid Input, You Gave : {}", input);
return;
}
}
2023-09-14 08:29:32 +03:00
}