本文整理汇总了Golang中net.IPConn.ReadMsgIP方法的典型用法代码示例。如果您正苦于以下问题:Golang IPConn.ReadMsgIP方法的具体用法?Golang IPConn.ReadMsgIP怎么用?Golang IPConn.ReadMsgIP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.IPConn
的用法示例。
在下文中一共展示了IPConn.ReadMsgIP方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: recvICMP
func (p *Pinger) recvICMP(conn *net.IPConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
p.debugln("recvICMP(): Start")
for {
select {
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
default:
}
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
p.debugln("recvICMP(): ReadMsgIP Start")
_, _, _, ra, err := conn.ReadMsgIP(bytes, nil)
p.debugln("recvICMP(): ReadMsgIP End")
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
p.debugln("recvICMP(): Read Timeout")
continue
} else {
p.debugln("recvICMP(): OpError happen", err)
p.mu.Lock()
ctx.err = err
p.mu.Unlock()
p.debugln("recvICMP(): close(ctx.done)")
close(ctx.done)
p.debugln("recvICMP(): wg.Done()")
wg.Done()
return
}
}
}
p.debugln("recvICMP(): p.recv <- packet")
select {
case recv <- &packet{bytes: bytes, addr: ra}:
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
}
}
}
示例2: readOneICMP4Packet
func readOneICMP4Packet(conn *net.IPConn, deadline time.Time) (
*icmpPacket, error,
) {
for {
conn.SetReadDeadline(deadline)
bytes := make([]byte, 512)
_, _, _, ra, err := conn.ReadMsgIP(bytes, nil)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
return nil, err
} else {
fmt.Println("readOneICMP4Packet(): OpError happen", err)
return nil, err
}
}
}
fmt.Printf("ICMP: Got packet from %s.\n", ra.String())
return &icmpPacket{bytes: bytes, addr: ra}, nil
}
}