doulu4976
2017-02-11 12:19
浏览 305
已采纳

在Golang中本地发送UDPv6

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).

图片转代码服务由CSDN问答提供 功能建议

我需要发送一个UDPv6数据报,以便能够由本地接收方(或通过 tcpdump来跟踪此消息) )。

  daddr,err = net.ResolveUDPAddr(“ udp6”,“ [address]:port”)
if err!= nil {
返回 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:无效参数。 当我尝试将其发送到[fe80 :: 20c:29ff:fee1:d66]:5000(根据 ifconfig 的我的IPv6地址)时,也会发生同样的情况。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchuza8908 2017-02-11 19:51
    已采纳

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题