我開發了一個使用 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 關注
- 244 瀏覽
添加回答
舉報
0/150
提交
取消