Crates.io | tcp_wrapper |
lib.rs | tcp_wrapper |
version | 0.2.2 |
source | src |
created_at | 2022-02-06 19:14:52.814294 |
updated_at | 2022-03-01 13:20:00.509849 |
description | It's a simple library to smart_house |
homepage | https://github.com/zhuravlevma |
repository | https://github.com/zhuravlevma/smart-house-lib |
max_upload_size | |
id | 527975 |
size | 27,842 |
[dependencies]
tcp_wrapper = "0.2"
tokio = { version = "1", features = ["full"] }
Then, on your main.rs:
use tcp_wrapper::client_async::TcpClient;
use tcp_wrapper::server_async::TcpServer;
use tcp_wrapper::server_async::Connection;
use std::thread;
use std::error::Error;
use tokio::spawn;
use tokio::join;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let tcp_server = TcpServer::bind("127.0.0.1:8085").await?;
join!(
server(tcp_server),
client()
);
Ok(())
}
async fn server(server: TcpServer) -> Result<(), Box<dyn Error>> {
let connection = server.accept().await?;
let req = connection.recv_request().await?;
assert_eq!(req, "Hello, server");
connection.send_response("Hello, client").await?;
Ok(())
}
async fn client() -> Result<(), Box<dyn Error>> {
let mut client = TcpClient::connect("127.0.0.1:8085").await?;
let response = client.send_request("Hello, server").await?;
assert_eq!(response, "Hello, client");
Ok(())
}
[dependencies]
tcp_wrapper = "0.2"
Then, on your main.rs:
use tcp_wrapper::client_std::Client;
use tcp_wrapper::server_std::TcpServer;
use std::error::Error;
use std::thread;
fn main() -> Result<(), Box<dyn Error>> {
let server = TcpServer::bind("127.0.0.1:9092")?;
thread::spawn(move || {
for elem in server.incoming() {
let mut e = elem.unwrap();
let req_str = e.recv_request().unwrap();
e.send_response("test").unwrap();
}
});
let mut client = Client::connect("127.0.0.1:9092")?;
let resp = client.send_request("hello").unwrap();
assert_eq!(resp, "test");
Ok(())
}
This project is licensed under the MIT license.