func benchmarkReadWriteIPv4UDP(b *testing.B, p *ipv4.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) { cm := ipv4.ControlMessage{TTL: 1} if ifi != nil { cm.IfIndex = ifi.Index } if n, err := p.WriteTo(wb, &cm, dst); err != nil { b.Fatal(err) } else if n != len(wb) { b.Fatalf("got %v; want %v", n, len(wb)) } if _, _, _, err := p.ReadFrom(rb); err != nil { b.Fatal(err) } }
func sender(p *ipv4.PacketConn, dst net.Addr) { ift, err := net.Interfaces() if err != nil { log.Fatal(err) } for i := 0; ; i++ { avail := net.FlagMulticast | net.FlagUp for _, ifi := range ift { if ifi.Flags&avail != avail { continue } p.SetMulticastInterface(&ifi) b := []byte(fmt.Sprintf("#%v HELLLO-R-U-THERE", i)) if _, err := p.WriteTo(b, nil, dst); err != nil { log.Println(err, "on", ifi) continue } log.Printf("%v bytes sent to %v via %v\n", len(b), dst, ifi) time.Sleep(time.Second) } } }