问题描述

我需要在多播UDP连接/套接字上设置IP_MULTICAST_LOOP,以便可以在本地计算机上发送/接收多播数据包.这是我发现的可以正常使用的电话:

I need to set IP_MULTICAST_LOOP on a multicast UDP connection/socket so I can send/receive multicast packets on my local machine. This is the call that I've found which should work:

l, err := net.ListenMulticastUDP("udp4", nil, addr)
file, err := l.File()
fd := syscall.Handle(file.Fd())

err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, 1)

但是,它失败并显示"Windows不支持".我很确定Windows的套接字支持此功能,而Go net程序包中不支持.关于如何在连接上设置此标志的任何想法吗?(我是Go语言的新手,我可能忽略了一些显而易见的东西).这是在Windows上,我还没有机会在Linux上进行测试.

However it fails with "not supported by windows". I'm pretty sure this is supported by sockets in windows, just not in the Go net package. Any idea on how I can set this flag on my connection? (I'm new to the Go language, I may have overlooked something obvious). This is on Windows, I haven't had a chance to test on Linux.

推荐答案

 net.ListenMulticastUDP  net.ListenMulticastUDP() IP_MULTICAST_LOOP 
net.ListenMulticastUDPnet.ListenMulticastUDP()IP_MULTICAST_LOOP
func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
    if ifi != nil {
        if err := setIPv4MulticastInterface(c.fd, ifi); err != nil {
            return err 
        }   
    }   
    if err := setIPv4MulticastLoopback(c.fd, false); err != nil {
        return err 
    }   
    if err := joinIPv4Group(c.fd, ifi, ip); err != nil {
        return err 
    }   
    return nil 
}
 setIPv4MulticastLoopback() sockoptip_windows.go 
setIPv4MulticastLoopback()sockoptip_windows.go
func setIPv4MulticastLoopback(fd *netFD, v bool) error {
    if err := fd.incref(); err != nil {
        return err 
    }   
    defer fd.decref()
    return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, boolint(v)))
}
 golang.org/x/net/ipv4  MulticastLoopback 
golang.org/x/net/ipv4MulticastLoopback
package main

import (
    "fmt"
    "net"
    "golang.org/x/net/ipv4"
)

func main() {
    ipv4Addr := &net.UDPAddr{IP: net.IPv4(224, 0, 0, 251), Port: 5352}
    conn, err := net.ListenUDP("udp4", ipv4Addr)
    if err != nil {
        fmt.Printf("ListenUDP error %v\n", err)
        return
    }

    pc := ipv4.NewPacketConn(conn)

    // assume your have a interface named wlan
    iface, err := net.InterfaceByName("wlan")
    if err != nil {
        fmt.Printf("can't find specified interface %v\n", err)
        return
    }
    if err := pc.JoinGroup(iface, &net.UDPAddr{IP: net.IPv4(224, 0, 0, 251)}); err != nil {
        return
    }

    // test
    if loop, err := pc.MulticastLoopback(); err == nil {
        fmt.Printf("MulticastLoopback status:%v\n", loop)
        if !loop {
            if err := pc.SetMulticastLoopback(true); err != nil {
            fmt.Printf("SetMulticastLoopback error:%v\n", err)
            }
        }
    }

    if _, err := conn.WriteTo([]byte("hello"), ipv4Addr); err != nil {
        fmt.Printf("Write failed, %v\n", err)
    }


    buf := make([]byte, 1024)
    for {
        if n, addr, err := conn.ReadFrom(buf); err != nil {
            fmt.Printf("error %v", err)
        } else {
            fmt.Printf("recv %s from %v\n", string(buf[:n]), addr)
        }
    }

    return
}

这篇关于如何在Golang中在多播UDPConn上设置IP_MULTICAST_LOOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!