我开发了一个使用 udp 多播发送和接收数据的示例,它在 linux 上工作但在 window10 上不工作。我尝试用Java开发另一个udp多播应用程序,它在相同的环境中工作!问题出在哪里?高朗代码:package mainimport ( "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 回答
UYOU
TA贡献1878条经验 获得超4个赞
问题是由“IP_MULTICAST_LOOP 选项的 Winsock 版本在语义上不同于 IP_MULTICAST_LOOP 选项的 UNIX 版本”引起的:
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
如何在 Golang 中的组播 UDPConn 上设置 IP_MULTICAST_LOOP
- 1 回答
- 0 关注
- 212 浏览
添加回答
举报
0/150
提交
取消