diff --git a/client/Cargo.toml b/client/Cargo.toml index df5436c..cf6d312 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -10,3 +10,4 @@ edition = "2021" image = { version = "0.24.7", features = [] } complex = { path = "../complex" } shared = { path = "../shared" } +server = { path = "../server" } diff --git a/client/src/client.rs b/client/src/client.rs index 8b13789..1bcf58b 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1 +1,32 @@ +use server::services::{connect::connect, write::write}; +/// Connect to the server and send a message +/// +/// # Arguments +/// +/// * `host` - Hostname +/// * `port` - Port +/// +/// # Return +/// +/// * `Result` - TcpStream +/// +/// # Example +/// +/// ``` +/// use client::client::connect_server; +/// +/// let stream = connect_server("localhost", "8080"); +/// ``` +/// +/// # Panic +/// +/// * `std::io::Error` - If the connection failed +/// +pub fn connect_server(host: &str, port: &str) -> std::io::Result { + let stream = connect(format!("{}:{}", host, port))?; + + write(stream, "Hello World !")?; + + Ok(()) +} diff --git a/server/Cargo.toml b/server/Cargo.toml index 32a59ae..df23bca 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -9,3 +9,6 @@ edition = "2021" [dependencies] complex = { path = "../complex" } shared = { path = "../shared" } + +[lib] +path = "src/lib.rs" diff --git a/server/src/mod.rs b/server/src/lib.rs similarity index 64% rename from server/src/mod.rs rename to server/src/lib.rs index efdff6d..c6b23f6 100644 --- a/server/src/mod.rs +++ b/server/src/lib.rs @@ -1,2 +1,3 @@ pub mod handler; pub mod image; +pub mod services; diff --git a/server/src/main.rs b/server/src/main.rs deleted file mode 100644 index b6dafd8..0000000 --- a/server/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("[SERVER] Hello, world!"); -} diff --git a/server/src/services/connect.rs b/server/src/services/connect.rs new file mode 100644 index 0000000..1ac1e2c --- /dev/null +++ b/server/src/services/connect.rs @@ -0,0 +1,17 @@ +use std::io::Result; +use std::net::TcpStream; + +/// Connect to a server +/// +/// # Arguments +/// +/// * `address` - Address +/// +/// # Return +/// +/// * `Result` - TcpStream +pub fn connect(address: &str) -> Result { + let stream = TcpStream::connect(address)?; + + Ok(stream) +} diff --git a/server/src/services/mod.rs b/server/src/services/mod.rs new file mode 100644 index 0000000..7f9fdb2 --- /dev/null +++ b/server/src/services/mod.rs @@ -0,0 +1,2 @@ +pub mod connect; +pub mod write; diff --git a/server/src/services/write.rs b/server/src/services/write.rs new file mode 100644 index 0000000..3e92d48 --- /dev/null +++ b/server/src/services/write.rs @@ -0,0 +1,19 @@ +use std::io::Result; +use std::io::Write; +use std::net::TcpStream; + +/// Write a string to a stream +/// +/// # Arguments +/// +/// * `stream` - TcpStream +/// * `message` - String +/// +/// # Return +/// +/// * `Result` - TcpStream +pub fn write(mut stream: TcpStream, message: &str) -> Result { + stream.write(format!("${}", message).as_bytes())?; + + Ok(stream) +}