标准库也缺乏对套接字和连接的细粒度控制,例如设置SO_REUSEADDR。mio库则提供了很多方法来解决这些问题。
依赖[dependencies]
mio = { version = "0.7.0", features = ["os-poll", "tcp"]}
源码示例
use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token};
const SERVER: Token = Token(0);
const CLIENT: Token = Token(1);
fn main() -> std::io::Result {
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(128);
// 创建server端socket
let addr = "127.0.0.1:8080".parse().unwrap();
let mut server = TcpListener::bind(addr)?;
// 开始监听该链接
poll.registry()
.register(&mut server, SERVER, Interest::READABLE)?;
// 创建客户端链接到服务端
let mut client = TcpStream::connect(addr)?;
// 监听客户端
poll.registry()
.register(&mut client, CLIENT, Interest::READABLE | Interest::WRITABLE)?;
// 启动循环
loop {
// 轮训事件
poll.poll(&mut events, None)?;
// 处理事件
let mut cnt = 0;
for event in events.iter() {
match event.token() {
SERVER => {
let connection = server.accept();
println!("SERVER recv a connection! cnt = {}", cnt);
drop(connection);
}
CLIENT => {
if event.is_writable() {
println!("CLIENT write, cnt = {}", cnt);
}
if event.is_readable() {
println!("CLIENT read, cnt = {}", cnt);
}
return Ok(());
}
_ => unreachable!(),
}
cnt += 1;
}
}
}
令狐壹冲
原创文章 157获赞 315访问量 4万+
关注
私信
展开阅读全文