2 streams but pure chaos

This commit is contained in:
nora 2021-03-29 10:54:14 +02:00
parent 04001ac5db
commit f6fcd8c05d
4 changed files with 104 additions and 12 deletions

9
Cargo.lock generated
View file

@ -1,5 +1,14 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "local_ipaddress"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6a104730949fbc4c78e4fa98ed769ca0faa02e9818936b61032d2d77526afa9"
[[package]] [[package]]
name = "terminal_chat" name = "terminal_chat"
version = "0.1.0" version = "0.1.0"
dependencies = [
"local_ipaddress",
]

View file

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
local_ipaddress = "0.1.3"

View file

@ -0,0 +1,38 @@
use std::net::{TcpStream, TcpListener, IpAddr, Ipv4Addr};
use std::io::{self, Read};
use std::sync::{Mutex, Arc};
use crate::IpAddress::{Normal, WithPort};
pub enum IpAddress {
WithPort(String, u32),
Normal(String)
}
pub fn connect(address: IpAddress) -> io::Result<TcpStream> {
let address = match address {
Normal(s) => {
match &*s {
"default" => String::from("localhost:7979"),
_ => s
}
},
WithPort(a, p) => format!("{}:{}", a, p)
};
TcpStream::connect(address)
}
pub fn listen(port: usize) -> io::Result<TcpStream> {
let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap();
println!("Listening on: {}:{}...", local_ipaddress::get().unwrap(), 7979);
listener.incoming().next().unwrap()
}
pub fn read_stream_print(mut stream: TcpStream) {
let mut data = [0 as u8; 50]; // using 50 byte buffer
for _ in stream.read(&mut data) {
println!("{}", String::from_utf8_lossy(&data));
}
}

View file

@ -1,24 +1,68 @@
use std::io::{self, Write}; use std::io::{self, Write, Read};
use terminal_chat::{connect, listen, read_stream_print, IpAddress};
use std::thread;
use std::sync::{Arc, Mutex};
fn main() { fn main() {
println!(" println!(r"
_______ _ _ _____ _ _ _______ _ _ _____ _ _
|__ __| (_) | |/ ____| | | | |__ __| (_) | |/ ____| | | |
| | ___ _ __ _ __ ___ _ _ __ __ _| | | | |__ __ _| |_ | | ___ _ __ _ __ ___ _ _ __ __ _| | | | |__ __ _| |_
| |/ _ \\ '__| '_ ` _ \\| | '_ \\ / _` | | | | '_ \\ / _` | __| | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | | | | '_ \ / _` | __|
| | __/ | | | | | | | | | | | (_| | | |____| | | | (_| | |_ | | __/ | | | | | | | | | | | (_| | | |____| | | | (_| | |_
|_|\\___|_| |_| |_| |_|_|_| |_|\\__,_|_|\\_____|_| |_|\\__,_|\\__| |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\_____|_| |_|\__,_|\__|
--------------------------------------------------------------- ---------------------------------------------------------------
Version 0.1"); Version 0.1");
let stdin = io::stdin();
println!("Do you want to listen(l) or connect(c) to a listener?");
let mut did_listen;
let stream =
if input().contains("l") {
did_listen = true;
listen(7979)
} else {
did_listen = false;
let mut address = input().trim().to_string();
println!("Trying to connect to '{}'", address.clone());
connect(IpAddress::Normal(address.clone()))
};
let stream = match stream {
Ok(x) => x,
Err(_) => {
println!("Error opening stream");
return;
}
};
let mut stream2 = match did_listen {
true => connect(IpAddress::WithPort(String::from("127.0.0.1"), 7980)).unwrap(),
false => listen(7989).unwrap()
};
thread::spawn(move || {
read_stream_print(stream);
});
println!("Connected.");
let connect_msg = "Hello World!";
stream2.write(connect_msg.as_bytes()).unwrap();
loop { loop {
print!("> "); let input = input();
io::stdout().flush(); stream2.write(input.clone().as_bytes());
let mut buffer = String::new(); println!("-- Trying to send {}", input);
stdin.read_line(&mut buffer);
println!("{}", buffer);
} }
} }
fn input() -> String {
print!("> ");
io::stdout().flush().unwrap();
let mut buffer = String::new();
let _ = io::stdin().read_line(&mut buffer);
buffer
}