diff --git a/jamp_client/src/main.rs b/jamp_client/src/main.rs index e7a11a9..f608f03 100644 --- a/jamp_client/src/main.rs +++ b/jamp_client/src/main.rs @@ -1,3 +1,14 @@ +use std::net::TcpStream; + fn main() { - println!("Hello, world!"); -} + if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") { + println!("Connected to the server!"); + let ip_result = stream.peer_addr(); + if ip_result.is_ok() { + println!("{}", ip_result.unwrap()); + + } + } else { + println!("Couldn't connect to server..."); + } +} \ No newline at end of file diff --git a/jamp_server/src/main.rs b/jamp_server/src/main.rs index e7a11a9..d31ea57 100644 --- a/jamp_server/src/main.rs +++ b/jamp_server/src/main.rs @@ -1,3 +1,29 @@ -fn main() { - println!("Hello, world!"); +use std::io::prelude::*; +use std::net::{TcpListener, TcpStream}; + +const IP: &str = "127.0.0.1:8080"; + +// https://doc.rust-lang.org/std/net/struct.TcpListener.html +fn handle_client(mut stream: TcpStream) { + let mut buffer = [0; 1024]; + + let read_result = stream.read(&mut buffer); + println!("read_result: {}", read_result.is_ok()); + + let shutdown_result = stream.shutdown(std::net::Shutdown::Both); + + println!("shutdown_result: {}", shutdown_result.is_ok()); +} + +// https://doc.rust-lang.org/std/net/struct.TcpListener.html +fn main() -> std::io::Result<()> { + let listener = TcpListener::bind(IP)?; + + println!("Listening on {}", IP); + + // accept connections and process them serially + for stream in listener.incoming() { + handle_client(stream?); + } + Ok(()) }