tcp_file_transfer
This commit is contained in:
parent
882eeb3050
commit
7f36889226
1 changed files with 210 additions and 0 deletions
210
main.rs
Normal file
210
main.rs
Normal file
|
@ -0,0 +1,210 @@
|
|||
use std::fs::File;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::io::{Read, Write, self};
|
||||
use std::env::{self};
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
file:Option<File>,
|
||||
location:String,
|
||||
bytes:Vec<u8>,
|
||||
size:usize,
|
||||
}
|
||||
impl FileInfo
|
||||
{
|
||||
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)
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
enum Connection
|
||||
{
|
||||
Server(String, String),
|
||||
Client(String, String),
|
||||
}
|
||||
impl Connection
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn main()
|
||||
{
|
||||
println!("Hello, world!");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//write_file(file_to_byte(read_file()));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue