本文整理汇总了Golang中golang.org/x/net/ipv4.PacketConn.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang PacketConn.Close方法的具体用法?Golang PacketConn.Close怎么用?Golang PacketConn.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/ipv4.PacketConn
的用法示例。
在下文中一共展示了PacketConn.Close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: udpReader
func udpReader(c *ipv4.PacketConn, ifname, hostPort string) {
log.Printf("udpReader: reading multicast")
defer c.Close()
buf := make([]byte, 10000)
for {
n, cm, _, err1 := c.ReadFrom(buf)
if err1 != nil {
log.Printf("udpReader: ReadFrom: error %v", err1)
break
}
ifi, err2 := net.InterfaceByIndex(cm.IfIndex)
if err2 != nil {
log.Printf("udpReader: could not solve ifindex=%d: %v", cm.IfIndex, err2)
}
ifname := "ifname?"
if ifi != nil {
ifname = ifi.Name
}
log.Printf("udpReader: recv %d bytes from %s to %s on %s (ifindex=%d)", n, cm.Src, cm.Dst, ifname, cm.IfIndex)
}
log.Printf("udpReader: exiting")
}
示例2: udpReader
func udpReader(c *ipv4.PacketConn, input chan<- *udpInfo, ifname string, readerDone chan<- int, listenPort int) {
log.Printf("udpReader: reading from '%s'", ifname)
defer c.Close()
buf := make([]byte, 10000)
LOOP:
for {
n, cm, srcAddr, err1 := c.ReadFrom(buf)
if err1 != nil {
log.Printf("udpReader: ReadFrom: error %v", err1)
break LOOP
}
var udpSrc *net.UDPAddr
switch srcAddr.(type) {
case *net.UDPAddr:
udpSrc = srcAddr.(*net.UDPAddr)
}
var name string
var ifi *net.Interface
var err2 error
if cm != nil {
ifi, err2 = net.InterfaceByIndex(cm.IfIndex)
if err2 != nil {
log.Printf("udpReader: unable to solve ifIndex=%d: error: %v", cm.IfIndex, err2)
}
}
if ifi == nil {
name = "ifname?"
} else {
name = ifi.Name
}
udpDst := net.UDPAddr{IP: cm.Dst, Port: listenPort}
//log.Printf("udpReader: recv %d bytes from %v to %v on %s ifIndex=%d", n, udpSrc, &udpDst, name, cm.IfIndex)
// make a copy because we will overwrite buf
b := make([]byte, n)
copy(b, buf)
// deliver udp packet to main rip goroutine
input <- &udpInfo{info: b, src: *udpSrc, dst: udpDst, ifIndex: cm.IfIndex, ifName: name}
}
log.Printf("udpReader: exiting '%s' -- trying", ifname)
readerDone <- 1 // tell rip router goroutine
log.Printf("udpReader: exiting '%s'", ifname)
}