无法读取UDP回复(golang)

我正在研究Go程序,该程序发出UDP广播以查询本地网络上是否存在设备,并且 然后阅读答复。 使用Wireshark,我确认该数据包已广播,并且(我)网络上的单个设备已答复(实际上是10次),但我的应用程序在读取时被阻止,好像它看不到传入的数据包。 这是代码:</ p>

  func Discover(timeout int)([] string,error){
inBuf:= make([] byte,1024)
devices: = make([] string,0)
var readLen int
var fromAddr * net.UDPAddr

//获取服务器连接
server:= fmt.Sprintf(“%s:%d”,bcastIP, udpDiscoverPort)//“ 255.255.255.255”,10000
serverAddr,err = net.ResolveUDPAddr(“ udp”,服务器)
checkErr(err)
ourAddr,err = net.ResolveUDPAddr(“ udp”,“ 192.168。 1.132:10000“)
checkErr(err)
conn,err = net.DialUDP(” udp“,ourAddr,serverAddr)
checkErr(err)
延迟conn.Close()

//发送 Discover消息
discoverMsg:= [] byte(magic)
discoverMsg = append(discoverMsg,Discovery ...)
sendLen,err:= conn.Write(discoverMsg)
checkErr(err)
fmt。 Println(“ Sent”,sendLen,“ bytes”)

//读取一个回复
readLen,fromAddr,err = conn.ReadFromUDP(inBuf)
fmt.Println(“ Read”,readLen,“ bytesfrom” ,fromAddr)
txtutil.Dump(string(inBuf [:readLen]))
返回设备,nil
}
</ code> </ pre>

checkErr(err)打印诊断信息,如果err不是nil,则退出BTW。</ p>

< p>答复中的信息如下:</ p>

  Internet协议版本4,Src:192.168.1.126(192.168.1.126),Dst:192.168.1.132(192.168.1.132)

用户数据报协议,源端口:ndmp(10000),目标端口:ndmp(10000)
</ code> </ pre>

我尝试了“ 0.0.0.0:10000”,“: 10000”和“ 127.0.0.1:10000”代替“ 192.168.1.132:10000”,似乎没有任何区别。 </ p>

欢迎提出任何关于我在做什么的建议!</ p>
</ div>

展开原文

原文

I'm working on a Go program that sends out a UDP broadcast to query existence of devices on the local network and then reads the replies. Using Wireshark I confirm that the packet is broadcast and that the single device on (my) network replies (ten times, in fact) but my application blocks on the read as if it does not see the incoming packet. Here is the code:

func Discover(timeout int) ([]string, error) {
    inBuf := make([]byte, 1024)
    devices := make([]string, 0)
    var readLen int
    var fromAddr *net.UDPAddr

    // get server connection
    server := fmt.Sprintf("%s:%d", bcastIP, udpDiscoverPort) // "255.255.255.255", 10000
    serverAddr, err = net.ResolveUDPAddr("udp", server)
    checkErr(err)
    ourAddr, err = net.ResolveUDPAddr("udp", "192.168.1.132:10000")
    checkErr(err)
    conn, err = net.DialUDP("udp", ourAddr, serverAddr)
    checkErr(err)
    defer conn.Close()

    // send the Discover message
    discoverMsg := []byte(magic)
    discoverMsg = append(discoverMsg, discovery...)
    sendLen, err := conn.Write(discoverMsg)
    checkErr(err)
    fmt.Println("Sent", sendLen, "bytes")

    // read one reply
    readLen, fromAddr, err = conn.ReadFromUDP(inBuf)
    fmt.Println("Read ", readLen, "bytesfrom ", fromAddr)
    txtutil.Dump(string(inBuf[:readLen]))
    return devices, nil
}

checkErr(err) prints a diagnostic and exits if err is not nil, BTW.

The information in the replies looks like:

Internet Protocol Version 4, Src: 192.168.1.126 (192.168.1.126), Dst: 192.168.1.132 (192.168.1.132)
User Datagram Protocol, Src Port: ndmp (10000), Dst Port: ndmp (10000)

I have tried "0.0.0.0:10000", ":10000" and "127.0.0.1:10000" in place of "192.168.1.132:10000" and none seem to make any difference.

Any suggestions as to what I'm doing wrong are welcome!

udp

1个回答



您需要使用 ListenUDP </ code>而不是 DialUDP </ code>。 当您使用 DialUDP </ code>时,它将创建一个“已连接”的UDP端口,并且仅在读取时返回源自远程地址的数据包。</ p>

  conn,  err = net.ListenUDP(“ udp”,ourAddr)
</ code> </ pre>

由于该连接没有默认目的地,因此您还需要使用 WriteTo * </ code>发送数据包的方法:</ p>

  sendLen,err:= conn.WriteToUDP(discoverMsg,serverAddr)
</ code> </ pre>
< / DIV>

展开原文

原文

You need to use ListenUDP instead of DialUDP. When you use DialUDP, it creates a "connected" UDP port, and only packets originating from the remote address are returned on read.

conn, err = net.ListenUDP("udp", ourAddr)

Since the connection doesn't have a default destination, you will also need to use WriteTo* methods to send packets:

sendLen, err := conn.WriteToUDP(discoverMsg, serverAddr)

doumeba0486
doumeba0486 我非常关注这个问题,甚至没有注意到糟糕的格式。 ;)
接近 4 年之前 回复
dra11767
dra11767 查看差异(stackoverflow.com/posts/40897417/revisions),我格式化了代码,使其正确显示。
接近 4 年之前 回复
douna6802
douna6802 这些更改有效。 我已经相应地标记了它。 编辑:我很好奇您在原始帖子中所做的编辑。 我看不到任何变化。
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问