英文:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论