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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败