Skip to content
Snippets Groups Projects
Select Git revision
  • dd3d25587001d8c17e7ff4094e1371c9300a248d
  • master default protected
2 results

backup.rs

Blame
  • backup.rs 3.06 KiB
    use std::io::prelude::*;
    use std::io::{stdout, stdin};
    use std::fmt;
    use std::net::{IpAddr, Ipv4Addr};
    use std::net::TcpStream;
    use std::net::TcpListener;
    
    
    //Message Size
    const BUFFER_SIZE: usize = 4;
    
    
    fn main() {
        //Listener
        let ip = IpAddr::V4(Ipv4Addr::new(10, 240, 200, 171));
        let port: u32 = 7777;
        //Sender
        let target_ip = IpAddr::V4(Ipv4Addr::new(130, 240, 200, 93));
        let target_port: u32 = 7777;
        //let ip = IpAddr::V4(Ipv4Addr::new(127,0,0,1));
        //let port: u32 = 7878;
        //Sender
        //let target_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        //let target_port: u32 = 7878;
    
        loop {
            print!("Please choose function:\n1. Send\n2. Receive\n====> ");
            stdout().flush().unwrap();
            let mut choice = String::new();
            stdin().read_line(&mut choice).expect("Incorrect input");
            let choice: u32 = match choice.trim().parse() {
                Ok(num) => num,
                Err(_) => continue,
            };
    
            print!("You picked: ");
            match choice {
                1 =>  { println!("Send!\nSending data...");
                    sender(target_ip, target_port);
                    println!("Data has been sent!\n");
                }
                2 => { println!("Listen!\nListening for data...");
                    listener(ip, port);
                    println!("Data has been listened to!\n");
                }
                _ => println!("Incorrect option!\n"),
            }
        }
        
        listener(ip, port);
        //sender(target_ip, target_port);
    }
    
    
    // Done
    fn sender(ip: IpAddr, port: u32) {
        
        //Sets up connection.
    	let mut stream = TcpStream::connect(format!("{}:{}", ip, port)).unwrap();
        let response = "This is a test sentence and will have only been successfully transmitted \
        if the text on screen show three x:es in a row at the very end. This marks the end of the test value. xxx";
    
        stream.write(response.as_bytes()).unwrap(); //Sends TCP data
        stream.flush().unwrap();
    }
    
    
    //Listens to TCP stream for data to be Printed in Console.
    fn listener(ip: IpAddr, port: u32){
        // Set up the listener to an IP and a Port.