advance-practice/io #880
Replies: 4 comments 3 replies
-
“在堆上分配缓冲区” 翻译的很赞,感觉英文版都没有讲清楚 |
Beta Was this translation helpful? Give feedback.
2 replies
-
这种固定长度因为其他原因需要储存在堆上的用 |
Beta Was this translation helpful? Give feedback.
0 replies
-
求教各位大佬, 标准库里的TcpStream可以实现split这样的操作么, 找了一圈都没找到咋实现的 |
Beta Was this translation helpful? Give feedback.
1 reply
-
Server 代码: use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::TcpListener,
};
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:6142").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = vec![0; 1024];
loop {
match socket.read(&mut buf).await {
Ok(0) => return,
Ok(n) => {
socket.write_all(&buf[..n]).await.unwrap();
}
Err(_) => return,
}
}
});
}
} 客户端代码: use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
#[tokio::main]
async fn main() -> io::Result<()> {
let socket = TcpStream::connect("127.0.0.1:6142").await?;
let (mut rd, mut wr) = io::split(socket);
tokio::spawn(async move {
wr.write_all(b"hello\r\n").await?;
wr.write_all(b"world\r\n").await?;
Ok::<_, io::Error>(())
});
let mut buf: Vec<u8> = vec![0; 128];
loop {
let n = rd.read(&mut buf).await?;
if n == 0 {
break;
}
println!("GOT {:?}", String::from_utf8(buf[..n].to_vec()).unwrap());
}
Ok(())
} 运行客户端后一直没有退出进程,打日志后发现是一直阻塞在 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
advance-practice/io
https://course.rs/advance-practice/io.html
Beta Was this translation helpful? Give feedback.
All reactions