当前位置: 首页>>代码示例>>Golang>>正文


Golang net.ListenMulticastUDP函数代码示例

本文整理汇总了Golang中net.ListenMulticastUDP函数的典型用法代码示例。如果您正苦于以下问题:Golang ListenMulticastUDP函数的具体用法?Golang ListenMulticastUDP怎么用?Golang ListenMulticastUDP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ListenMulticastUDP函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewServer

// NewServer is used to create a new mDNS server from a config
func NewServer(config *Config) (*Server, error) {
	// Create the listeners
	ipv4List, err := net.ListenMulticastUDP("udp4", config.Iface, ipv4Addr)
	if err != nil {
		log.Printf("[ERR] mdns: Failed to start IPv4 listener: %v", err)
	}
	ipv6List, err := net.ListenMulticastUDP("udp6", config.Iface, ipv6Addr)
	if err != nil {
		log.Printf("[ERR] mdns: Failed to start IPv6 listener: %v", err)
	}

	// Check if we have any listener
	if ipv4List == nil && ipv6List == nil {
		return nil, fmt.Errorf("No multicast listeners could be started")
	}

	s := &Server{
		config:     config,
		ipv4List:   ipv4List,
		ipv6List:   ipv6List,
		shutdownCh: make(chan struct{}),
	}
	go s.recv(s.ipv4List)
	go s.recv(s.ipv6List)
	return s, nil
}
开发者ID:KensakuKOMATSU,项目名称:mdns,代码行数:27,代码来源:server.go

示例2: NewMDNS

func NewMDNS(hostname string, services map[string]int) (MDNS, error) {
	rrs, err := localRR(hostname, services)
	if err != nil {
		return nil, err
	}

	connv4, err := net.ListenMulticastUDP("udp4", nil, ipv4mcastaddr)
	if err != nil {
		return nil, err
	}

	connv6, err := net.ListenMulticastUDP("udp6", nil, ipv6mcastaddr)
	if err != nil {
		connv6 = nil
	}

	m := &mdns{
		local:   rrs,
		connv4:  connv4,
		connv6:  connv6,
		pending: []*query{},
		queries: make(chan *query, 64),
		exit:    make(chan bool, 0),
	}

	qst := make(chan msg, 0)
	go m.loop(qst)
	go m.readloop(connv4, qst)
	go m.readloop(connv6, qst)

	return m, nil
}
开发者ID:zserge,项目名称:mdns,代码行数:32,代码来源:mdns.go

示例3: NewServer

// NewServer is used to create a new mDNS server from a config
func NewServer(config *Config) (*Server, error) {
	// Create the listeners
	ipv4List, _ := net.ListenMulticastUDP("udp4", config.Iface, ipv4Addr)
	ipv6List, _ := net.ListenMulticastUDP("udp6", config.Iface, ipv6Addr)

	// Check if we have any listener
	if ipv4List == nil && ipv6List == nil {
		return nil, fmt.Errorf("No multicast listeners could be started")
	}

	s := &Server{
		config:     config,
		ipv4List:   ipv4List,
		ipv6List:   ipv6List,
		shutdownCh: make(chan struct{}),
	}

	if ipv4List != nil {
		go s.recv(s.ipv4List)
	}

	if ipv6List != nil {
		go s.recv(s.ipv6List)
	}

	return s, nil
}
开发者ID:bmatsuo,项目名称:mdns,代码行数:28,代码来源:server.go

示例4: openSocket

func openSocket(addr *net.UDPAddr) (*net.UDPConn, error) {
	switch addr.IP.To4() {
	case nil:
		return net.ListenMulticastUDP("udp6", nil, ipv6mcastaddr)
	default:
		return net.ListenMulticastUDP("udp4", nil, ipv4mcastaddr)
	}
	panic("unreachable")
}
开发者ID:davecheney,项目名称:mdns,代码行数:9,代码来源:mdns.go

示例5: newServer

// Constructs server structure
func newServer(iface *net.Interface) (*server, error) {
	// Create wildcard connections (because :5353 can be already taken by other apps)
	ipv4conn, err := net.ListenMulticastUDP("udp4", iface, mdnsWildcardAddrIPv4)
	if err != nil {
		log.Printf("[ERR] bonjour: Failed to bind to udp4 port: %v", err)
	}
	ipv6conn, err := net.ListenMulticastUDP("udp6", iface, mdnsWildcardAddrIPv6)
	if ipv4conn == nil && ipv6conn == nil {
		return nil, fmt.Errorf("[ERR] bonjour: Failed to bind to any udp port!")
	}

	// Join multicast groups to receive announcements
	p1 := ipv4.NewPacketConn(ipv4conn)
	p2 := ipv6.NewPacketConn(ipv6conn)
	if iface != nil {
		errCount := 0
		if err := p1.JoinGroup(iface, &net.UDPAddr{IP: mdnsGroupIPv4}); err != nil {
			errCount++
		}
		if err := p2.JoinGroup(iface, &net.UDPAddr{IP: mdnsGroupIPv6}); err != nil {
			errCount++
		}
		if errCount == 2 {
			return nil, fmt.Errorf("Failed to join multicast group on both v4 and v6")
		}
	} else {
		ifaces, err := net.Interfaces()
		if err != nil {
			return nil, err
		}
		errCount1, errCount2 := 0, 0
		for _, iface := range ifaces {
			if err := p1.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv4}); err != nil {
				errCount1++
			}
			if err := p2.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv6}); err != nil {
				errCount2++
			}
		}
		if len(ifaces) == errCount1 && len(ifaces) == errCount2 {
			return nil, fmt.Errorf("Failed to join multicast group on all interfaces!")
		}
	}

	s := &server{
		ipv4conn:   ipv4conn,
		ipv6conn:   ipv6conn,
		shutdownCh: make(chan bool),
	}

	return s, nil
}
开发者ID:anlaneg,项目名称:socketplane,代码行数:53,代码来源:server.go

示例6: setupSockets

func (conn *Connection) setupSockets() (err error) {
	// NOTE(bo): On the IP address used for send and receive connections.
	//
	// Go only sets SO_REUSEADDR and SO_REUSEPORT when using a Multicast
	// IP address[1][2][3]. Without dropping down to C, there isn't a way great around
	// this.
	//
	// Despite not being the 255.255.255.255 used in other LIFX libraries, In
	// practice, it seems to work for receiving messages. Sending messages is
	// still somewhat unverified.
	//
	// I may be confusing multicast and broadcast a bit here, but I don't know how else to
	// enable binding to the same interface and port multiple times...
	//
	// [1]: http://golang.org/src/pkg/net/sock_posix.go?h=setDefaultMulticastSockopts#L161
	// [2]: http://golang.org/src/pkg/net/sockopt_bsd.go (also ./sockopt_linux.go)
	// [3]: http://en.wikipedia.org/wiki/Multicast_address#Local_subnetwork
	ip := net.IPv4(224, 0, 0, 1)

	peer, err := net.ListenMulticastUDP("udp4", nil, &net.UDPAddr{
		IP:   ip,
		Port: peerPort,
	})
	if err != nil {
		return
	}

	broadcast, err := net.ListenMulticastUDP("udp4", nil, &net.UDPAddr{
		IP:   ip,
		Port: broadcastPort,
	})
	if err != nil {
		return
	}

	write, err := net.DialUDP("udp4", nil, &net.UDPAddr{
		IP:   ip,
		Port: broadcastPort,
	})
	if err != nil {
		return
	}

	conn.sockets.peer = peer
	conn.sockets.broadcast = broadcast
	conn.sockets.write = write

	return
}
开发者ID:ninjasphere,项目名称:go-lifx,代码行数:49,代码来源:connections.go

示例7: registryLookupServiceOnInterface

// Server which listens for incoming multicast requests on the specified interface. Upon receive of a
// request it sends the registry address to the asking client.
func registryLookupServiceOnInterface(address *net.TCPAddr, intf net.Interface, ch chan int) {
	response := LookupAddressResponse{*address}
	buffer := make([]byte, PACKET_SIZE)

	connection, err := net.ListenMulticastUDP(UDP_PROTOCOL, &intf, MULTICAT_ADDR)
	if err != nil {
		return
	}
	defer connection.Close()

	bytes, err := json.Marshal(response)
	if err != nil {
		return
	}

	for {
		_, sender, err := connection.ReadFromUDP(buffer)
		if err != nil {
			return
		}
		_, err = connection.WriteToUDP(bytes, sender)
		if err != nil {
			return
		}
	}

	ch <- 0
}
开发者ID:jzipfler,项目名称:HTW-SwArchitektur,代码行数:30,代码来源:service.go

示例8: Listen_mc

/*
	Joins a multicast group as a listener on the named interface
*/
func (this *Cmgr) Listen_mc(ifname string, addr string, data2usr chan *Sess_data) (sessid string, err error) {

	sessid = ""
	iface, err := net.InterfaceByName(ifname)
	if err != nil {
		return
	}

	sessid = fmt.Sprintf("m%d", this.mcount) // successful bind to port
	this.mcount++

	uaddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		return
	}

	uconn, err := net.ListenMulticastUDP("udp", iface, uaddr)
	if err != nil {
		return
	}

	cp := new(connection)
	cp.conn = nil
	cp.uconn = uconn
	cp.data2usr = data2usr  // session data written to this channel
	cp.id = sessid          // user assigned session id
	this.clist[sessid] = cp // hash for write to session

	go this.conn_reader(cp) // start reader; will discard if data2usr is nil

	return sessid, err
}
开发者ID:ScottDaniels,项目名称:gopkgs,代码行数:35,代码来源:connman.go

示例9: main

func main() {
	maddr, err := net.ResolveUDPAddr("udp", "239.0.0.1:1234")
	if err != nil {
		log.Fatalln("Failed to resolve UDP addr")
	}

	mconn, err := net.ListenMulticastUDP("udp", nil, maddr)
	if err != nil {
		log.Fatalln("Failed to listen at UDP addr")
	}

	wconn, err := net.DialUDP("udp", nil, maddr)
	if err != nil {
		log.Fatalln("Failed to blah")
	}

	buf := make([]byte, 96)
	for i := 0; i < 10; i++ {
		n, err := mconn.Read(buf)

		if err != nil {
			log.Fatalln("Failed to read from UDP conn")
		}

		log.Printf("Read %v bytes: %v\n", n, string(buf[:n]))

		n, err = wconn.Write(append([]byte("pong"), buf[:n]...))

		if err != nil {
			log.Fatalln("Failed to write to UDP conn", err)
		}
	}
}
开发者ID:rodolf0,项目名称:snippets,代码行数:33,代码来源:upd-multicast.go

示例10: Listen

func Listen(service Service, addr, port string) {
	buffSize := service.BuffSize()

	ServerAddr, err := net.ResolveUDPAddr("udp", addr+":"+port)
	if err != nil {
		log.Println("ERROR: discovery.Listen ResolveUDPAddr: ", err)
		return
	}

	ServerConn, err := net.ListenMulticastUDP("udp", nil, ServerAddr)
	defer ServerConn.Close()
	ServerConn.SetReadBuffer(buffSize)
	if err != nil {
		log.Println("ERROR: discovery.Listen ListenUDP: ", err)
		return
	}
	service.SetPort(Port(ServerConn.LocalAddr()))

	for {
		buf := make([]byte, buffSize)
		n, addr, err := ServerConn.ReadFromUDP(buf)
		if err != nil {
			log.Println("ERROR: discovery.Listen ReadFromUDP: ", err)
			continue
		}
		buf = bytes.Trim(buf, "\x00")
		buf, err = crypto.Decrypt(service, buf)
		if err != nil {
			log.Println("ERROR: discovery.Listen Decrypt: ", err)
			continue
		}
		service.Handle(buf, n, addr)
	}
}
开发者ID:pdxjohnny,项目名称:discovery,代码行数:34,代码来源:discovery.go

示例11: bind

func (a *Server) bind() (conn *net.UDPConn, err error) {
	if !a.NotMulticast && a.iface.Flags&net.FlagMulticast == net.FlagMulticast {
		gaddr, err := a.groupAddr()
		if err != nil {
			return nil, e.Forward(err)
		}
		conn, err = net.ListenMulticastUDP(a.Proto(), a.iface, gaddr)
		if err != nil {
			return nil, e.New(err)
		}
	} else {
		server, err := net.ResolveUDPAddr(a.Proto(), ":"+a.Port)
		if err != nil {
			return nil, e.New(err)
		}
		conn, err = net.ListenUDP(a.Proto(), server)
		if err != nil {
			return nil, e.New(err)
		}
	}
	a.ipver(conn.LocalAddr())
	_, a.Port, err = utilNet.SplitHostPort(conn.LocalAddr().String())
	if err != nil {
		return nil, e.Forward(err)
	}
	return
}
开发者ID:fcavani,项目名称:discover,代码行数:27,代码来源:server.go

示例12: Bind

// Bind starts listening on udp addr.
// if emitter is true, bind to :0 and write to c.addr
// else bind to c.addr and read from c.addr
func (c *Conn) Bind(emitter bool) error {
	var addr *net.UDPAddr
	var conn *net.UDPConn
	var err error

	if emitter {
		addr, err = net.ResolveUDPAddr("udp", ":0")
	} else {
		addr = c.addr
	}

	if err != nil {
		return err
	}

	if addr.IP != nil && addr.IP.IsMulticast() {
		conn, err = net.ListenMulticastUDP("udp", nil, addr)
	} else {
		conn, err = net.ListenUDP("udp", addr)
	}

	if err != nil {
		return err
	}

	c.socket = conn
	return nil
}
开发者ID:bhenderson,项目名称:lwes-go,代码行数:31,代码来源:conn.go

示例13: NewNetInput

func NewNetInput(address string, port int) (*NetInput, error) {
	var err error
	var conn *net.UDPConn

	n := &NetInput{}
	n.addr = net.UDPAddr{
		Port: port,
		IP:   net.ParseIP(address),
	}
	n.input = nil

	// the stream may be unicast or multicast, so choose appropriately
	if n.addr.IP.IsMulticast() {
		conn, err = net.ListenMulticastUDP("udp", nil, &n.addr)
	} else {
		conn, err = net.ListenUDP("udp", &n.addr)
	}
	if err != nil {
		conn.Close()
		return nil, err
	}
	n.conn = conn

	go n.process()
	return n, nil
}
开发者ID:aktungmak,项目名称:chq,代码行数:26,代码来源:NetInput.go

示例14: main

func main() {
	maddr, err := net.ResolveUDPAddr("udp", "239.255.43.99:1090")
	check(err)
	fmt.Printf("%#v\n", maddr)
	conn, err := net.ListenMulticastUDP("udp", nil, maddr)
	check(err)
	go func(conn *net.UDPConn) {
		for {
			buf := make([]byte, 12)
			now := time.Now()
			conn.SetDeadline(now.Add(time.Second * 5))
			n, addr, err := conn.ReadFromUDP(buf)
			fmt.Println("\ninput from udp:", buf, n, addr, err)
		}
	}(conn)

	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print("Input: ")
		t, _, _ := reader.ReadLine()
		if len(t) == 0 {
			fmt.Println("Bye")
			os.Exit(0)
		}
		fmt.Println("Got input:", t)
		b := make([]byte, 12)
		copy(b, t)
		n, err := conn.WriteToUDP(b, maddr)
		check(err)
		fmt.Println("Sent to udp:", n, err)
	}

}
开发者ID:xinghuwang,项目名称:toolbox,代码行数:33,代码来源:multicast.go

示例15: udpAddMcastRX

func udpAddMcastRX(addr string) error {
	udpaddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		return err
	}

	// have we already added this ctx?
	for _, c := range defaultUDPConfig.rxers {
		if c.addr == udpaddr {
			return nil
		}
	}

	// XXX: ability to specify interfaces
	// XXX: may need more sophisticated handling for multicast
	udpconn, err := net.ListenMulticastUDP("udp", nil, udpaddr)
	if err != nil {
		return err
	}

	println("adding mcast:", addr)

	rxer := udpCtx{udpconn, udpaddr}
	defaultUDPConfig.rxers = append(defaultUDPConfig.rxers, rxer)

	go rxer.rx()
	return nil
}
开发者ID:liamstask,项目名称:go-rtps,代码行数:28,代码来源:udp.go


注:本文中的net.ListenMulticastUDP函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。