This commit is contained in:
nora 2021-03-30 21:53:45 +02:00
parent f006bab748
commit 10e8813f9d
3 changed files with 102 additions and 62 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "terminal_chat"
version = "0.1.0"
authors = ["Nilstrieb <nils.heydecker@gmx.ch>"]
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -1,29 +1,50 @@
use std::net::{TcpStream, TcpListener};
use std::io::{self, Read};
use crate::IpAddress::{Normal, WithPort};
use std::net::{TcpListener, TcpStream, IpAddr, SocketAddr, Ipv4Addr, SocketAddrV4};
use std::error::Error;
use std::str::FromStr;
pub enum IpAddress {
WithPort(String, u32),
Normal(String)
type EmptyResult = Result<(), Box<dyn Error>>;
pub fn listen() -> EmptyResult {
let l_stream = listen_on_port(7979)?;
println!("Connected. Waiting for response stream...");
let other_address = l_stream.peer_addr()?.ip();
println!("other adress: {}", other_address);
let s_stream = connect_to_addr("127.0.0.1", 7980)?;
Ok(())
}
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)
};
pub fn connect(address: String, port: String) -> EmptyResult {
println!("Trying to connect to '{}:{}'", &address, &port);
TcpStream::connect(address)
let port: u16 = port.parse()?;
let s_stream = connect_to_addr(&*address, port)?;
println!("Connected. Waiting for response stream...");
let l_stream = listen_on_port(7080)?;
println!("Connected. Chat is now open...");
Ok(())
}
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);
pub fn connect_to_addr(address: &str, port: u16) -> Result<TcpStream, Box<dyn Error>> {
let socket_addr = SocketAddrV4::new(Ipv4Addr::from_str(address)?, port);
match TcpStream::connect(socket_addr) {
Err(e) => Err(Box::new(e)),
Ok(s) => Ok(s)
}
}
pub fn listen_on_port(port: u16) -> io::Result<TcpStream> {
let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();
println!(
"Listening on: {}:{}...",
local_ipaddress::get().unwrap(),
port
);
listener.incoming().next().unwrap()
}
@ -35,3 +56,10 @@ pub fn read_stream_print(mut stream: TcpStream) {
println!("{}", String::from_utf8_lossy(&data));
}
}
fn input() -> String {
let mut buffer = String::new();
let _ = io::stdin().read_line(&mut buffer);
buffer
}

View file

@ -1,10 +1,12 @@
use std::io::{self, Write};
use terminal_chat::{connect, listen, read_stream_print, IpAddress};
use std::thread;
use terminal_chat::{
connect, listen, listen_on_port, read_stream_print,
};
fn main() {
println!(r"
println!(
r"
_______ _ _ _____ _ _
|__ __| (_) | |/ ____| | | |
| | ___ _ __ _ __ ___ _ _ __ __ _| | | | |__ __ _| |_
@ -12,21 +14,34 @@ fn main() {
| | __/ | | | | | | | | | | | (_| | | |____| | | | (_| | |_
|_|\___|_| |_| |_| |_|_|_| |_|\__,_|_|\_____|_| |_|\__,_|\__|
---------------------------------------------------------------
Version 0.1");
Version 0.1"
);
println!("Do you want to listen(l) or connect(c) to a listener?");
let 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 result = if input().contains("l") {
listen()
} else {
println!("Address: (empty for default)");
let address = match &*input() {
"" => String::from("127.0.0.1"),
s => String::from(s)
};
println!("Port: (empty for default)");
let port = match &*input() {
"" => String::from("7979"),
s => String::from(s)
};
connect(address, port)
};
match result {
Ok(_) => println!("Exited TerminalChat sucessfully."),
Err(e) => println!("An error occurred! Error message: '{}'", e)
}
// old
/*
let stream = match stream {
Ok(x) => x,
@ -37,8 +52,8 @@ Version 0.1");
};
let mut stream2 = match did_listen {
true => connect(IpAddress::WithPort(String::from("127.0.0.1"), 7980)).unwrap(),
false => listen(7989).unwrap()
true => connect_to_addr(IpAddress::WithPort(String::from("127.0.0.1"), 7980)).unwrap(),
false => listen_on_port(7989).unwrap(),
};
thread::spawn(move || {
@ -54,13 +69,10 @@ Version 0.1");
let input = input();
stream2.write(input.clone().as_bytes());
println!("-- Trying to send {}", input);
}*/
}
}
fn input() -> String {
print!("> ");
io::stdout().flush().unwrap();
let mut buffer = String::new();
let _ = io::stdin().read_line(&mut buffer);
buffer