dongzheng4556 2018-03-03 13:23
浏览 212
已采纳

绑定:无法在本地网络中的UDPclient上分配请求的地址

As I'm finding quite some examples how to write a go server and client, this works locally on one machine.

Now I'm trying to communicate in my local network between two PCs, one running the go server script, one the client.

However, I can't establish a connection because of the error:

Error: listen udp 192.168.11.6:10001: bind: cannot assign requested address
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x401376] // ...

Of course I will post my code (client, where the problem occurs):

package main

import (
    "fmt"
    "net"
    "strconv"
    "time"
)

func CheckError(err error) {
    if err != nil {
        fmt.Println("Error: ", err)
    }
}

func main() {
    ServerAddr, err := net.ResolveUDPAddr("udp", "192.168.11.6:10001")
    CheckError(err)

    Conn, err := net.ListenUDP("udp", ServerAddr)
    CheckError(err)

    defer Conn.Close()
    i := 0
    for {
        msg := strconv.Itoa(i)
        i++
        buf := []byte(msg)
        _, err = Conn.WriteToUDP(buf, ServerAddr)
        time.Sleep(time.Second * 1)
    }
}

Server:

package main
import (
    "fmt"
    "net"
    "os"
)

/* A Simple function to verify error */
func CheckError(err error) {
    if err  != nil {
        fmt.Println("Error: " , err)
        os.Exit(0)
    }
}

func main() {
    ServerAddr,err := net.ResolveUDPAddr("udp",":10001")
    CheckError(err)

    ServerConn, err := net.ListenUDP("udp", ServerAddr)
    CheckError(err)
    defer ServerConn.Close()

    buf := make([]byte, 1024)
    for {
        fmt.Println("Starting...")
        n,addr,err := ServerConn.ReadFromUDP(buf)
        fmt.Println("Received ",string(buf[0:n]), " from ",addr)
        ServerConn.WriteToUDP([]byte("hello there!"), addr)
        if err != nil {
            fmt.Println("Error: ",err)
        }
    }
}

The client has the local network IP address 192.168.11.8 and the server 192.168.11.6. They can also ping each other, and I'm opening the port when Windows asks for it.

I'm happy about all suggestions. I struggle with this because I only find localhost server client go examples.

  • 写回答

1条回答 默认 最新

  • doubenggua9430 2018-03-03 16:05
    关注

    Okay, I figured it out. Weird that I had to allow the app to communicate on public networks? While I am on my home network. When starting the server - I had to enable public networks

    I figured out that I do not have to open a socket on the client side, but use DialUDP

    Also the server needed the full Ip address in
    ServerAddr,err := net.ResolveUDPAddr("udp","192.168.11.6:10001")

    Improved Client code: (main func)

    ServerAddr, err := net.ResolveUDPAddr("udp", "192.168.11.6:10001")
    CheckError(err)
    buf := make([]byte, 1024)
    Conn, err := net.DialUDP("udp", nil, ServerAddr)
    CheckError(err)
    
    defer Conn.Close()
    i := 0
    for {
        msg := strconv.Itoa(i)
        i++
        fmt.Printf(msg)
        n, err := Conn.Write([]byte(msg))
        CheckError(err)
        fmt.Printf("sent %d bytes", n)
        n, addr, err := Conn.ReadFromUDP(buf)
        if err == nil {
            fmt.Printf("%s %s
    ", buf, addr)
        } else {
            fmt.Printf("some err %v
    ", err)
        }
        time.Sleep(time.Second * 1)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题