diff --git a/Cargo.toml b/Cargo.toml index eaafa18..807103d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,5 @@ edition = "2021" tokio = { version = "1.45.1", features = ["full"] } tokio-modbus = "0.16.1" tokio-serial = "5.4.1" -bytes = "1.0" futures = "0.3.31" tokio-util = "0.7.15" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7c5a77e..7a1b1fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ use tokio::net::{UnixListener, UnixStream}; -use tokio_util::codec::{Framed, LinesCodec}; +//use tokio_util::codec::{Framed, LinesCodec}; use futures::SinkExt; use tokio::io::AsyncReadExt; +//use tokio::io::AsyncReadExt; -const SOCKET_PATH: &str = "/tmp/rust_domain_socket.sock"; +const SOCKET_PATH: &str = "/tmp/modbus.sock"; #[tokio::main] async fn main() -> Result<(), Box> { // 绑定Unix Domain Socket @@ -26,21 +27,15 @@ async fn main() -> Result<(), Box> { } async fn handle_client(mut stream: UnixStream) -> Result<(), Box> { + let len = stream.read_u16().await.unwrap(); + let mut buf = vec![0u8; len as usize]; - // 使用行编解码器 - let mut framed = Framed::new(stream, LinesCodec::new()); + match stream.read_exact(&mut buf).await { + Ok(_) => { - // 发送欢迎消息 - framed.send("Welcome to Rust Domain Socket Server").await?; - - // 处理客户端消息 - while let Some(message) = framed.next().await { - match message { - Ok(msg) => { - println!("Received: {}", msg); - framed.send(format!("Echo: {}", msg)).await?; - } - Err(e) => return Err(e.into()), + }, + Err(err) => { + println!("read data get error: {}", err) } }