gpt4 book ai didi

asynchronous - 我如何 read_until future 链中的 tokio::net::TcpStream?

转载 作者:可可西里 更新时间:2023-11-01 02:36:51 26 4
gpt4 key购买 nike

我想从 TcpStream 读取数据,直到遇到“\0”。问题是 tokio::io::read_until 需要流为 BufRead

fn poll(&mut self) -> Poll<(), Self::Error> {
match self.listener.poll_accept()? {
Async::Ready((stream, _addr)) => {
let task = tokio::io::read_until(stream, 0, vec![0u8; buffer])
.map_err(|_| ...)
.map(|_| ...);
tokio::spawn(task);
}
Async::NotReady => return Ok(Async::NotReady),
}
}

如何通过这种方式从 TcpStream 中读取数据?

最佳答案

阅读 BufRead 的文档, 你会看到文字:

If you have something that implements Read, you can use the BufReader type to turn it into a BufRead.

fn example(stream: TcpStream) {
io::read_until(std::io::BufReader::new(stream), 0, vec![]);
}

关于asynchronous - 我如何 read_until future 链中的 tokio::net::TcpStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com