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

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));
}
}