本文簡要介紹rust語言中 std::net::TcpListener.into_incoming
的用法。
用法
pub fn into_incoming(self) -> IntoIncoming
將其轉換為在此偵聽器上接收到的連接的迭代器。
返回的迭代器永遠不會返回 None
,也不會產生對等點的 SocketAddr
結構。迭代它相當於在循環中調用 TcpListener::accept
。
例子
#![feature(tcplistener_into_incoming)]
use std::net::{TcpListener, TcpStream};
fn listen_on(port: u16) -> impl Iterator<Item = TcpStream> {
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.into_incoming()
.filter_map(Result::ok) /* Ignore failed connections */
}
fn main() -> std::io::Result<()> {
for stream in listen_on(80) {
/* handle the connection here */
}
Ok(())
}
相關用法
- Rust TcpListener.incoming用法及代碼示例
- Rust TcpListener.take_error用法及代碼示例
- Rust TcpListener.accept用法及代碼示例
- Rust TcpListener.local_addr用法及代碼示例
- Rust TcpListener.ttl用法及代碼示例
- Rust TcpListener.set_ttl用法及代碼示例
- Rust TcpListener.bind用法及代碼示例
- Rust TcpListener.try_clone用法及代碼示例
- Rust TcpListener.set_nonblocking用法及代碼示例
- Rust TcpListener用法及代碼示例
- Rust TcpStream.local_addr用法及代碼示例
- Rust TcpStream.peer_addr用法及代碼示例
- Rust TcpStream.set_nodelay用法及代碼示例
- Rust TcpStream.nodelay用法及代碼示例
- Rust TcpStream.take_error用法及代碼示例
- Rust TcpStream.write_timeout用法及代碼示例
- Rust TcpStream.peek用法及代碼示例
- Rust TcpStream.set_write_timeout用法及代碼示例
- Rust TcpStream.set_nonblocking用法及代碼示例
- Rust TcpStream.set_linger用法及代碼示例
- Rust TcpStream用法及代碼示例
- Rust TcpStream.linger用法及代碼示例
- Rust TcpStream.connect用法及代碼示例
- Rust TcpStream.ttl用法及代碼示例
- Rust TcpStream.shutdown用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::net::TcpListener.into_incoming。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。