英文:
Sending UDPv6 locally in golang
问题
我需要发送一个UDPv6数据报,并能够通过本地接收器(或通过tcpdump
)跟踪此消息。
daddr, err = net.ResolveUDPAddr("udp6", "[address]:port")
if err != nil {
return err
}
conn, err := net.DialUDP("udp6", nil, daddr)
if err != nil {
return err
}
defer conn.Close()
conn.Write(...)
与IPv4不同,这段代码在IPv6上不起作用。例如,当我尝试发送一个数据报到多播地址,例如[FF01::DB8:0:0]:5000时,我会得到connect: invalid argument
的错误。当我尝试将其发送到[fe80::20c:29ff:fee1:d66]:5000(根据ifconfig
显示的我的IPv6地址)时,也会出现相同的错误。
英文:
I need to send a UDPv6 datagram being able to track this message by a local receiver (or via tcpdump
).
daddr, err = net.ResolveUDPAddr("udp6", "[address]:port")
if err != nil {
return err
}
conn, err := net.DialUDP("udp6", nil, daddr)
if err != nil {
return err
}
defer conn.Close()
conn.Write(...)
Unlike IPv4 this code doesn't work with IPv6. For example, when I try to send a datagram to a multicast address, e.g. to [FF01::DB8:0:0]:5000, I get connect: invalid argument
. The same happens when I try to send it to [fe80::20c:29ff:fee1:d66]:5000 (my IPv6 address according to ifconfig
).
答案1
得分: 3
在这两种情况下(链路本地和接口本地多播),您忘记了指定范围ID。没有范围ID,无法确定使用哪个接口,操作系统会返回“无效参数”错误。
net.UDPAddr
使用 Zone
字段来存储范围ID。您需要确保提供了一个范围ID,可以通过显式设置 Zone
或使用 百分号后缀表示法 来实现。
英文:
In both cases (link-local and interface-local multicast) you have forgotten to specify the scope ID. Without this, it is impossible to determine which interface to use, and you get an Invalid argument
error from the operating system.
net.UDPAddr
uses the Zone
field to store the scope ID. You need to ensure that you have provided one, either by setting Zone
explicitly or by using the percent-suffix notation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论