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

udp
duandang2838
duandang2838 您没有加入要向其发送的多播组。
接近 5 年之前 回复
dongya8378
dongya8378 你打电话给socket.JoinGroup吗?
接近 5 年之前 回复

1个回答



您是否尝试在其他主机上收听它? </ p>

Go(至少在1.4版中)对多播进行了硬编码的回送禁用。 这意味着您看不到自己的数据包。</ p>

您可以通过在套接字FD本身上调用setsockopt来覆盖此数据包,或者:</ p>

golang.org/x/net/ipv6 库可以 </ p>

或者您可以从这里(必须四处寻找)</ p>
</ div>

展开原文

原文

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)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐