blob: 9db4edf64b19690737ef15a8c09ac85084c661df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//! A example to show how to use UnixStream.
use local_sync::oneshot::channel;
use monoio::{
io::{AsyncReadRent, AsyncWriteRentExt},
net::{UnixListener, UnixStream},
};
const ADDRESS: &str = "/tmp/monoio-unix-test.sock";
#[monoio::main]
async fn main() {
let (mut tx, rx) = channel::<()>();
monoio::spawn(async move {
tx.closed().await;
let mut client = UnixStream::connect(ADDRESS).await.unwrap();
let buf = "hello";
let (ret, buf) = client.write_all(buf).await;
ret.unwrap();
println!("write {} bytes: {buf:?}", buf.len());
});
std::fs::remove_file(ADDRESS).ok();
let listener = UnixListener::bind(ADDRESS).unwrap();
println!("listening on {ADDRESS:?}");
drop(rx);
let (mut conn, addr) = listener.accept().await.unwrap();
println!("accepted a new connection from {addr:?}");
let buf = Vec::with_capacity(1024);
let (ret, buf) = conn.read(buf).await;
ret.unwrap();
println!("read {} bytes: {buf:?}", buf.len());
// clear the socket file
drop(listener);
std::fs::remove_file(ADDRESS).ok();
}
|