Golang无法将UDP数据包发送到多播组。

huangapple go评论106阅读模式
英文:

Golang doesn't send UDP packet to multicast group

问题

目前我正在尝试用Go重新实现一个C应用程序。C应用程序的一部分是将字符串发送到多播组。这会通过tcpdump捕获以下数据包:

00000000  d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00  |................|
00000010  ff ff 00 00 01 00 00 00  14 81 06 56 47 2c 01 00  |...........VG,..|
00000020  46 00 00 00 46 00 00 00  33 33 00 02 10 01 04 ce  |F...F...33......|
00000030  ef ca fe 1a 86 dd 60 00  00 00 00 10 11 01 fe 80  |......`.........|
00000040  00 00 00 00 00 00 06 ce  ef ff fe ca fe 1a ff 02  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 02 10 01 be 8f  |................|
00000060  03 e9 00 10 99 68 6e 6f  64 65 69 6e 66 6f        |.....hnodeinfo|

我尝试使用以下代码复制这个行为:

const MultiCastGroup string = "ff02:0:0:0:0:0:2:1001"
const Port int = 1001
const Proto string = "udp6"
const MaxDataGramSize int = 8192

var announcedAddr = &net.UDPAddr{IP: net.ParseIP(MultiCastGroup), Port: Port}

buf := []byte("nodeinfo")
unicastConn, _ := net.ListenUDP(Proto, &net.UDPAddr{IP: net.IPv6zero, Port: 0})
unicastConn.WriteToUDP(buf, announcedAddr)

但我认为它没有起作用,因为我通过tcpdump只能捕获到以下内容:

00000000  d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00  |................|
00000010  ff ff 00 00 01 00 00 00                           |........|
00000018

似乎连数据包都没有发送出去。我在Debian Wheezy机器上尝试了这个。有人在使用golang和UDP时遇到类似的问题吗?

提前感谢。

英文:

currently I try to reimplement a C application in go. Part of the C application is to send a string to a multicast group. This produces the following packet captured via tcpdump:

00000000  d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00  |................|
00000010  ff ff 00 00 01 00 00 00  14 81 06 56 47 2c 01 00  |...........VG,..|
00000020  46 00 00 00 46 00 00 00  33 33 00 02 10 01 04 ce  |F...F...33......|
00000030  ef ca fe 1a 86 dd 60 00  00 00 00 10 11 01 fe 80  |......`.........|
00000040  00 00 00 00 00 00 06 ce  ef ff fe ca fe 1a ff 02  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 02 10 01 be 8f  |................|
00000060  03 e9 00 10 99 68 6e 6f  64 65 69 6e 66 6f        |.....hnodeinfo|

I tried to replicate the behavior with the following code:

const MultiCastGroup string = "ff02:0:0:0:0:0:2:1001"
const Port int = 1001
const Proto string = "udp6"
const MaxDataGramSize int = 8192

var announcedAddr = &net.UDPAddr{IP: net.ParseIP(MultiCastGroup), Port: Port}

buf := []byte("nodeinfo")
unicastConn, _ := net.ListenUDP(Proto, &net.UDPAddr{IP: net.IPv6zero, Port: 0})
unicastConn.WriteToUDP(buf, announcedAddr)

But the I don't think that it is working, because all I can capture from this via tcpdump is:

00000000  d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00  |................|
00000010  ff ff 00 00 01 00 00 00                           |........|
00000018

It doesn't seem that the packet is even send. I tried this on a Debian Wheezy machine. Did anyone if you encounter a similar problem with golang and UDP?

Thanks in advance

答案1

得分: 3

你是否尝试在另一台主机上进行监听?

Go语言(至少在1.4版本中)在多播上有一个硬编码的禁用回环功能。这意味着你无法看到自己发送的数据包。

你可以通过在套接字的文件描述符上调用setsockopt来覆盖此设置,或者使用golang.org/x/net/ipv6库来实现。

你也可以从这里获取代码(需要查找一下)。

英文:

Did you try to listen for it on another host?

Go (at least in 1.4) had a hard coded disable of loopback on multicast. Which means that, you can't see your own packets.

You can override this by calling setsockopt on the socket FD itself, or:

The golang.org/x/net/ipv6 library can do this for you.

Or you can take the code from Here (have to poke around to find it)

huangapple
  • 本文由 发表于 2015年9月26日 20:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/32797164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定