[英]Is tokio rust resetting tcp connections?
I am writing a simple server in rust to understand a bit better async tokyo and tcp connections, there are fifty connections and first they write a hello and then they read, and it seems to work fine but when I go to the resource monitor in windows和 select 我的两个程序我看到连接数开始随着一些奇怪的尖峰而变化。
这是我的服务器
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
};
#[tokio::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:62000").await.unwrap();
loop {
let (socket, addr) = listener.accept().await.unwrap();
println!("new connection from {}", addr);
handle(socket).await;
}
}
async fn handle(mut socket: TcpStream) {
tokio::spawn(async move {
let mut buffer = [0; 1024];
loop {
socket.write_all(b"hello").await.unwrap();
socket.read(&mut buffer).await.unwrap();
}
});
}
在这里我开始我的客户
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
#[tokio::main]
async fn main() {
let mut handles = Vec::new();
for _ in 0..50 {
let mut socket = TcpStream::connect("127.0.0.1:62000").await.unwrap();
println!(
"connected to {} from {}",
socket.peer_addr().unwrap(),
socket.local_addr().unwrap()
);
let handle = tokio::spawn(async move {
let mut buffer = [0; 1024];
loop {
socket.write_all(b"hello").await.unwrap();
socket.read(&mut buffer).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
}
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.