mirror of
https://github.com/Noratrieb/terminal-chat.git
synced 2026-01-14 08:30:13 +01:00
2 streams but pure chaos
This commit is contained in:
parent
04001ac5db
commit
f6fcd8c05d
4 changed files with 104 additions and 12 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -1,5 +1,14 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# 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]]
|
||||
name = "terminal_chat"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"local_ipaddress",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
local_ipaddress = "0.1.3"
|
||||
|
|
|
|||
38
src/lib.rs
38
src/lib.rs
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
66
src/main.rs
66
src/main.rs
|
|
@ -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() {
|
||||
println!("
|
||||
println!(r"
|
||||
_______ _ _ _____ _ _
|
||||
|__ __| (_) | |/ ____| | | |
|
||||
| | ___ _ __ _ __ ___ _ _ __ __ _| | | | |__ __ _| |_
|
||||
| |/ _ \\ '__| '_ ` _ \\| | '_ \\ / _` | | | | '_ \\ / _` | __|
|
||||
| |/ _ \ '__| '_ ` _ \| | '_ \ / _` | | | | '_ \ / _` | __|
|
||||
| | __/ | | | | | | | | | | | (_| | | |____| | | | (_| | |_
|
||||
|_|\\___|_| |_| |_| |_|_|_| |_|\\__,_|_|\\_____|_| |_|\\__,_|\\__|
|
||||
|_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\_____|_| |_|\__,_|\__|
|
||||
---------------------------------------------------------------
|
||||
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 {
|
||||
let input = input();
|
||||
stream2.write(input.clone().as_bytes());
|
||||
println!("-- Trying to send {}", input);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn input() -> String {
|
||||
print!("> ");
|
||||
io::stdout().flush();
|
||||
io::stdout().flush().unwrap();
|
||||
let mut buffer = String::new();
|
||||
|
||||
stdin.read_line(&mut buffer);
|
||||
|
||||
println!("{}", buffer);
|
||||
}
|
||||
let _ = io::stdin().read_line(&mut buffer);
|
||||
buffer
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue