同WiFi环境下Rust Web服务器跨设备访问失败问题排查求助
跨设备访问Rust Web服务器失败的排查求助
更新: 不明原因下重启Mac mini后,Axum和Actix服务器均能被iPhone及Windows正常访问,暂未明确问题根源。
我近期遇到一个异常网络问题,已进行多轮排查仍未找到原因:
环境信息
- 开发设备:运行macOS Sonoma 14.1.2的Mac mini,IP
192.168.50.150 - 同WiFi设备:iOS 17.3的iPhone(
192.168.50.196)、Windows 10笔记本(192.168.50.169) - 基础连通性:iPhone和Windows均可ping通Mac,Mac防火墙已关闭
问题复现
在Mac上通过VS Code终端运行Axum框架的Rust Web服务器代码:
use axum::routing::get; #[tokio::main] async fn main() { let app = axum::Router::new() .route("/", get(root)); let listener = tokio::net::TcpListener::bind("0.0.0.0:23232").await.unwrap(); axum::serve(listener, app).await.unwrap(); } async fn root() -> &'static str { "Hello from Rust" }
本地可通过 http://192.168.50.150:23232/ 访问,但无法从iPhone和Windows跨设备访问。
对比测试(正常工作的服务器)
关闭Rust服务器后,运行Node.js服务器代码:
const express = require("express"); var app = express(); app.get("/", function (req, res) { res.send("Hello from nodejs"); }); app.listen(23232, function () { console.log("Running!"); });
所有设备均可通过同一URL正常访问;使用 python3 -m http.server 23232 启动的Python服务器也能正常跨设备访问。
更换为Actix框架的Rust服务器仍无法跨设备访问:
use actix_web::{get, App, HttpServer, Responder}; #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new().service(index)) .bind(":::23232")? .run() .await } #[get("/")] async fn index() -> impl Responder { "Hello from Rust Actix" }
排查细节
- 端口绑定对比:Rust服务器初始显示为
tcp4,Node.js服务器显示为tcp46;将Rust服务器绑定地址改为":::23232"后,netstat输出变为tcp46,但跨设备访问问题仍存在。debug和release版本均有此问题。 - 访问状态差异:
- 关闭Rust服务器时,访问立即报错:iPhone显示
Safari can't open the page because it couldn't connect to the server.,Windows显示This site can't be reached. 192.168.50.150 refused to connect.(错误码ERR_CONNECTION_REFUSED) - 开启Rust服务器后,浏览器持续加载直至超时,Windows浏览器显示
ERR_CONNECTION_TIMED_OUT
- 关闭Rust服务器时,访问立即报错:iPhone显示
- 流量捕获:tcpdump显示运行Rust服务器时,Mac能收到iPhone的请求,但无响应;运行Node.js服务器时,Mac与iPhone能正常完成TCP握手及数据交互。
重启Mac后问题意外解决,现求助该问题的可能原因及后续排查方向。
内容的提问来源于stack exchange,提问作者sudoExclamationExclamation
相关产品推荐
相关产品推荐

