0

我正在编写 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);
}
4

0 回答 0