Golang在Windows 10中无法使用UDP组播。

huangapple go评论99阅读模式
英文:

Golang UDP multicast not working in windows10

问题

我开发了一个使用UDP多播发送和接收数据的示例,在Linux上工作正常,但在Windows 10上不起作用。我尝试用Java开发另一个UDP多播应用程序,在相同的环境中工作!问题出在哪里?

以下是Golang代码的翻译:

package main

import (
	"flag"
	"fmt"
	"net"
	"strings"
)

var (
	bind = flag.String("bind", "239.255.0.0", "绑定地址")
	port = flag.Int("port", 2222, "端口")
	cmd  = flag.String("cmd", "server", "命令")
)

func main() {
	flag.Parse()
	addr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", *bind, *port))

	if err != nil {
		panic(err)
	}
	switch *cmd {
	case "server":
		{
			startServer(addr)
		}
	case "client":
		{
			startClient(addr, strings.Join(flag.Args(), ""))
		}
	}

}

func startServer(addr *net.UDPAddr) {
	conn, err := net.ListenMulticastUDP("udp4", nil, addr)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("服务器已启动")
	}
	var buff = make([]byte, 1600)
	for {
		l, remoteAddr, err := conn.ReadFromUDP(buff)
		if err != nil {
			panic(err)
		}
		fmt.Printf("从 %v 读取数据,数据内容:%s\n", remoteAddr, string(buff[0:l]))
	}
}

func startClient(addr *net.UDPAddr, msg string) {
	conn, err := net.DialUDP("udp4", nil, addr)
	if err != nil {
		panic(err)
	}

	l, err := conn.Write([]byte(msg))
	if err != nil {
		panic(err)
	} else {
		fmt.Printf("写入字节长度:%d\n", l)
	}
	conn.Close()
}

希望对你有帮助!如果还有其他问题,请随时提问。

英文:

I developed an example that uses udp multicast to send and receive data,it work at linux but not wort at window10.
I try to developed another udp multicast application by Java, it works in the same environment!
Where is the problem?

Golang code:

package main
import (
"flag"
"fmt"
"net"
"strings"
)
var (
bind = flag.String("bind", "239.255.0.0", "bind")
port = flag.Int("port", 2222, "port")
cmd  = flag.String("cmd", "server", "Command")
)
func main() {
flag.Parse()
addr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", *bind, *port))
if err != nil {
panic(err)
}
switch *cmd {
case "server":
{
startServer(addr)
}
case "client":
{
startClient(addr, strings.Join(flag.Args(), ""))
}
}
}
func startServer(addr *net.UDPAddr) {
conn, err := net.ListenMulticastUDP("udp4", nil, addr)
if err != nil {
panic(err)
} else {
fmt.Println("Server was started")
}
var buff = make([]byte, 1600)
for {
l, remoteAddr, err := conn.ReadFromUDP(buff)
if err != nil {
panic(err)
}
fmt.Printf("read data from %v, data: %s\n", remoteAddr, string(buff[0:l]))
}
}
func startClient(addr *net.UDPAddr, msg string) {
conn, err := net.DialUDP("udp4", nil, addr)
if err != nil {
panic(err)
}
l, err := conn.Write([]byte(msg))
if err != nil {
panic(err)
} else {
fmt.Printf("Wrote byte length %d\n", l)
}
conn.Close()
}

答案1

得分: 0

尤里卡!问题是由于“IP_MULTICAST_LOOP选项的Winsock版本与IP_MULTICAST_LOOP选项的UNIX版本在语义上有所不同”引起的:

在Winsock中,IP_MULTICAST_LOOP选项仅适用于接收路径。
在UNIX版本中,IP_MULTICAST_LOOP选项适用于发送路径。

https://learn.microsoft.com/en-us/windows/win32/winsock/ip-multicast-2

https://stackoverflow.com/questions/43109552/how-to-set-ip-multicast-loop-on-multicast-udpconn-in-golang

英文:

Eureka!
The problem was caused by "The Winsock version of the IP_MULTICAST_LOOP option is semantically different than the UNIX version of the IP_MULTICAST_LOOP option":

In Winsock, the IP_MULTICAST_LOOP option applies only to the receive path.
In the UNIX version, the IP_MULTICAST_LOOP option applies to the send path.

https://learn.microsoft.com/en-us/windows/win32/winsock/ip-multicast-2

https://stackoverflow.com/questions/43109552/how-to-set-ip-multicast-loop-on-multicast-udpconn-in-golang

huangapple
  • 本文由 发表于 2022年7月21日 15:23:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/73062193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定