You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust WebSocket客户端连接报错os error 10053求助

Rust WebSocket客户端连接报错os error 10053求助

各位大佬好!我最近刚学Rust,写了个简单的WebSocket客户端搭配echo服务器,结果客户端一直连不上,报下面这个错误:

WebSocket server ws://127.1.0.1:8080/ connection failed : IO error: An established connection was aborted by the software in your host machine. (os error 10053)

我已经折腾了好一阵,排查了服务器状态、防火墙、端口占用这些常规问题,但都没解决,甚至在Linux虚拟机上试了同样的代码也报类似错误,现在怀疑是不是自己的Rust代码里有什么没注意到的细节问题?

先贴我的客户端代码:

use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use tokio::select;
use anyhow::Result;
use futures_util::{SinkExt, StreamExt};
use url::Url;

#[tokio::main]
async fn main() -> Result<()> {
    let url = Url::parse("ws://127.1.0.1:8080")?;
    let ret = connect_async(url.as_str()).await;
    match ret {
        Ok((ws_stream, _)) => {
            println!("WebSocket client connected");

            let (mut write, mut read) = ws_stream.split();

            for _i in 0..100 {
                select! {
                    // Receiving messages from the server
                    Some(msg) = read.next() => {
                        match msg? {
                            Message::Text(text) => {
                                println!("Received message from server: {}", text);
                            }
                            Message::Binary(bin) => {
                                println!("Received binary message from server: {:?}", bin);
                            }
                            Message::Close(_) => {
                                println!("WebSocket connection closed by server");
                                break;
                            }
                            Message::Ping(ping) => {
                                println!("Received ping from server: {:?}", ping);
                                match write.send(Message::Pong(ping)).await {
                                    Ok(_) => {},
                                    Err(e) => println!("Error sending pong response: {}", e),
                                }
                            }
                            Message::Pong(pong) => {
                                println!("Received pong from server: {:?}", pong);
                            }
                            _ => {}
                        }
                    }
                    else => {
                        println!("WebSocket connection closed");
                        break;
                    }
                }
            }
        },
        Err(e) => {
            println!("WebSocket server {} connection failed : {}", url, e);
            return Ok(());
        }
    }

    Ok(())
}

我的Cargo.toml依赖配置:

tokio = { version = "1.50.0", features = ["full"] }
tokio-tungstenite = "0.28.0"
futures = "0.3"
url = "2"
anyhow = "1.0"
futures-util = "0.3"

我已经试过的排查步骤:

  • 确认服务器进程正常运行,无日志报错
  • 关闭了Windows防火墙,Linux虚拟机上也禁用了防火墙规则
  • 用netstat/ss命令确认8080端口确实被服务器进程监听
  • 更换过多个其他空闲端口,问题依旧存在

有没有大佬能帮我定位下问题?感激不尽!

备注:内容来源于stack exchange,提问作者user3570501

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:08:06