mirror of
https://github.com/Noratrieb/terminal-chat.git
synced 2026-01-15 17:05:10 +01:00
rewrite
This commit is contained in:
parent
f006bab748
commit
10e8813f9d
3 changed files with 102 additions and 62 deletions
68
src/lib.rs
68
src/lib.rs
|
|
@ -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()
|
||||
}
|
||||
|
|
@ -34,4 +55,11 @@ pub fn read_stream_print(mut stream: TcpStream) {
|
|||
for _ in stream.read(&mut data) {
|
||||
println!("{}", String::from_utf8_lossy(&data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn input() -> String {
|
||||
let mut buffer = String::new();
|
||||
let _ = io::stdin().read_line(&mut buffer);
|
||||
buffer
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue