连接被对等方重置。东京,选择
我正在写socks5代理服务器。该程序正在异步运行,我尝试使用 tokio::select
,但是当我想获取接收到的数据的大小时,程序由于此错误而终止:
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 54, kind: ConnectionReset, message: "Connection reset by peer" }'
发生错误的函数:
async fn exchange_loop(mut client: TcpStream, address: SocketAddr, cmd: u8) {
let mut remote = TcpStream::connect(address).await.unwrap();
let mut buffer_client: [u8; 4096] = [0; 4096];
let mut buffer_remote: [u8; 4096] = [0; 4096];
loop {
tokio::select! {
size = client.read(&mut buffer_client) => {
let size = size.unwrap();
remote.write(buffer_client.as_ref()).await.unwrap();
println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
if size <= 0 {
break;
};
buffer_client = [0; 4096];
}
size = remote.read(&mut buffer_remote) => {
let size = size.unwrap();
client.write(buffer_remote.as_ref()).await.unwrap();
println!("Send from remote {} => {} {} KB", address, client.peer_addr().unwrap(), size as f32 / 1024.);
if size <= 0 {
break;
};
buffer_remote = [0; 4096];
}
}
}
println!("End connection to {}", address);
}
I am writing socks5 proxy server. The program is running asynchronously and I am trying to use tokio::select
, but the program terminates due to this error when I want to get the size of the received data:
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 54, kind: ConnectionReset, message: "Connection reset by peer" }'
The function where the error occurs:
async fn exchange_loop(mut client: TcpStream, address: SocketAddr, cmd: u8) {
let mut remote = TcpStream::connect(address).await.unwrap();
let mut buffer_client: [u8; 4096] = [0; 4096];
let mut buffer_remote: [u8; 4096] = [0; 4096];
loop {
tokio::select! {
size = client.read(&mut buffer_client) => {
let size = size.unwrap();
remote.write(buffer_client.as_ref()).await.unwrap();
println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
if size <= 0 {
break;
};
buffer_client = [0; 4096];
}
size = remote.read(&mut buffer_remote) => {
let size = size.unwrap();
client.write(buffer_remote.as_ref()).await.unwrap();
println!("Send from remote {} => {} {} KB", address, client.peer_addr().unwrap(), size as f32 / 1024.);
if size <= 0 {
break;
};
buffer_remote = [0; 4096];
}
}
}
println!("End connection to {}", address);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要处理错误,如前面的注释中所指定。您需要能够从客户端所做的任何操作中恢复,例如中断连接。
如何恢复的示例:
我输入 Ok(_) 因为我不知道您对第二次解包有何期望。
You need to handle errors, as specified in the previous comments. You need to be able to recover from whatever the client does on his side, interrupting the connection for example.
An example how to recover:
I put Ok(_) as I do not know what you expect from the 2nd unwrap.