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

Go UDP局域网跨PC通信报错:bind: cannot assign requested address及空指针异常

Hey Daniel, let’s work through this issue—this is a really common hurdle when moving Go network apps from local testing to actual LAN communication! Let’s break down the errors and fix them one by one.

1. Resolve the "cannot assign requested address" bind error

This is the root cause of your initial failure, and it’s almost always tied to how your server is binding to a network address:

  • Bind to 0.0.0.0 instead of a specific IP
    When you bind to a specific LAN IP like 192.168.11.6, your server will only listen on that exact network interface. If the IP is incorrect, or the interface isn’t active, you’ll get this error. Instead, bind to 0.0.0.0 (which listens on all available network interfaces) using:
    addr, err := net.ResolveUDPAddr("udp", ":10001")
    // Or explicitly use IPv4zero:
    // addr := &net.UDPAddr{IP: net.IPv4zero, Port: 10001}
    if err != nil {
        log.Fatal(err)
    }
    conn, err := net.ListenUDP("udp", addr)
    
  • Verify your server’s actual LAN IP
    Double-check that 192.168.11.6 is truly the IP of your server machine. Run ipconfig (Windows) or ip addr (Linux/macOS) to confirm the correct IPv4 address for your local network adapter.
  • Check for port conflicts
    Make sure no other program is using port 10001. On Windows, run netstat -ano | findstr :10001; on Linux/macOS, use lsof -i :10001 to see if the port is occupied.
2. Fix the nil pointer dereference panic

This panic happens because your code is trying to use the *UDPConn returned by ListenUDP without checking for errors first. If ListenUDP fails (like with the bind error), it returns nil for the connection, and any subsequent call to that nil connection will trigger the panic. Always add error checking immediately after the listen call:

conn, err := net.ListenUDP("udp", addr)
if err != nil {
    log.Fatalf("Failed to start UDP server: %v", err)
}
defer conn.Close() // Ensure the connection is closed when done
3. Additional LAN connectivity checks

Even after fixing the bind issue, you might still have connectivity problems—here’s what to verify:

  • Allow the port through firewalls
    Both your server and client machines need to allow UDP traffic on port 10001. On Windows, create an inbound/outbound firewall rule for the port; on Linux, use sudo ufw allow 10001/udp; on macOS, adjust firewall settings in System Preferences.
  • Client connects to the correct server IP
    Make sure your client code is targeting the server’s actual LAN IP (not localhost or 127.0.0.1). For example:
    serverAddr, err := net.ResolveUDPAddr("udp", "192.168.11.6:10001")
    if err != nil {
        log.Fatal(err)
    }
    conn, err := net.DialUDP("udp", nil, serverAddr)
    
  • Confirm protocol consistency
    Double-check that both server and client are using UDP—mixing TCP and UDP will cause silent failures or errors.

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:05:28