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) =>
|
|
|
|
{
|
2023-09-16 21:58:21 +03:00
|
|
|
match File::write_all(file_descriptor, &mut self.bytes)
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
2023-09-16 22:08:10 +03:00
|
|
|
Ok(_) =>
|
|
|
|
{
|
|
|
|
self.size = self.bytes.len();
|
|
|
|
println!("Done: Write -> {} bytes", &mut self.size);
|
|
|
|
}
|
2023-09-16 21:58:21 +03:00
|
|
|
Err(err_val) => println!("Error: Write {}", err_val),
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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![];
|
2023-09-16 21:58:21 +03:00
|
|
|
match stream.read_to_end(&mut data)
|
2023-09-16 19:15:30 +03:00
|
|
|
{
|
|
|
|
Ok(res) =>
|
|
|
|
{
|
|
|
|
if res == 0
|
|
|
|
{
|
|
|
|
println!("Connection Closed");
|
|
|
|
return;
|
|
|
|
}
|
2023-09-16 21:58:21 +03:00
|
|
|
file_info.bytes = data;
|
|
|
|
FileInfo::write_file(file_info);
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
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);
|
2023-09-16 21:58:21 +03:00
|
|
|
socket.write_all(&file_info.bytes).unwrap();
|
2023-09-16 19:15:30 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|