douquan1953 2015-09-26 12:27
浏览 157

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条回答

  • duanqiu9104 2015-09-26 14:43
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services