github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/net/udpsock_plan9_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package net 6 7 import ( 8 "internal/testenv" 9 "runtime" 10 "testing" 11 ) 12 13 func TestListenMulticastUDP(t *testing.T) { 14 testenv.MustHaveExternalNetwork(t) 15 16 ifcs, err := Interfaces() 17 if err != nil { 18 t.Skip(err.Error()) 19 } 20 if len(ifcs) == 0 { 21 t.Skip("no network interfaces found") 22 } 23 24 var mifc *Interface 25 for _, ifc := range ifcs { 26 if ifc.Flags&FlagUp|FlagMulticast != FlagUp|FlagMulticast { 27 continue 28 } 29 mifc = &ifc 30 break 31 } 32 33 if mifc == nil { 34 t.Skipf("no multicast interfaces found") 35 } 36 37 c1, err := ListenMulticastUDP("udp4", mifc, &UDPAddr{IP: ParseIP("224.0.0.254")}) 38 if err != nil { 39 t.Fatalf("multicast not working on %s", runtime.GOOS) 40 } 41 c1addr := c1.LocalAddr().(*UDPAddr) 42 if err != nil { 43 t.Fatal(err) 44 } 45 defer c1.Close() 46 47 c2, err := ListenUDP("udp4", &UDPAddr{IP: IPv4zero, Port: 0}) 48 c2addr := c2.LocalAddr().(*UDPAddr) 49 if err != nil { 50 t.Fatal(err) 51 } 52 defer c2.Close() 53 54 n, err := c2.WriteToUDP([]byte("data"), c1addr) 55 if err != nil { 56 t.Fatal(err) 57 } 58 if n != 4 { 59 t.Fatalf("got %d; want 4", n) 60 } 61 62 n, err = c1.WriteToUDP([]byte("data"), c2addr) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if n != 4 { 67 t.Fatalf("got %d; want 4", n) 68 } 69 }