Skip to content

macOS 上端口占用判断不准确 #119

Description

@AnemoFlower

在 macOS 环境下,即使端口转发的本地端口已被占用,Terracotta 依然会尝试使用这个端口。
相关代码和日志片段:

Terracotta/src/ports.rs

Lines 12 to 17 in 22bdce0

impl PortRequest {
pub fn request_specific(port: u16) -> io::Result<u16> {
TcpListener::bind((Ipv4Addr::LOCALHOST, port))
.and_then(|socket| socket.local_addr())
.map(|address| address.port())
}

// To maximum compatibility, try to request the identical port.
// If failed, use a dynamic free port instead.
let local_port = PortRequest::request_specific(port).unwrap_or_else(|e| {
logging!("RoomExperiment", "Unable to request shadow port {} on client: {:?}. Mods requiring UDP socket like SimpleVoiceChat may go wrong.", port, e);
PortRequest::Minecraft.request()
});

[RoomExperiment]: Scaffolding Server has been verified.
[RoomExperiment]: MC server is at 25565
[EasyTier CLI]: Port forward rule add: udp 0.0.0.0:25565
[EasyTier CLI]: Port forward rule add: tcp [::]:25565
[EasyTier CLI]: Error: Rust error: Failed to reload port forwards
[EasyTier CLI]: 
[EasyTier CLI]: Caused by:
[EasyTier CLI]:     0: tunnel error io error
[EasyTier CLI]:     1: io error
[EasyTier CLI]:     2: Address already in use (os error 48)
[EasyTier CLI]: 
[EasyTier CLI]: Caused by:
[EasyTier CLI]:     Failed to reload port forwards
[EasyTier CLI]:     
[EasyTier CLI]:     Caused by:
[EasyTier CLI]:         0: tunnel error io error
[EasyTier CLI]:         1: io error
[EasyTier CLI]:         2: Address already in use (os error 48)
[EasyTier CLI]: Cannot adding port-forward rules: 0.0.0.0:25565 -> 10.126.126.1:25565 (tcp), [::]:25565 -> 10.126.126.1:25565 (udp)
[RoomExperiment]: Cannot create a port-forward 25565 -> 25565 for MC Connection.
最小可复现示例
use std::io;
use std::net::{Ipv4Addr, TcpListener};

fn request_specific(port: u16) -> io::Result<u16> {
    TcpListener::bind((Ipv4Addr::LOCALHOST, port))
        .and_then(|socket| socket.local_addr())
        .map(|address| address.port())
}

fn main() {
    let port = 25565;

    match request_specific(port) {
        Ok(port) => println!("绑定成功:{}", port),
        Err(e) => println!("绑定失败:{}", e),
    }
}
Image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    macosOnly Apple Can Do

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions