本文整理汇总了Golang中net.IPConn.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang IPConn.Write方法的具体用法?Golang IPConn.Write怎么用?Golang IPConn.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.IPConn
的用法示例。
在下文中一共展示了IPConn.Write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Pinger
func Pinger(address *net.IPAddr, timeout time.Duration) error {
var (
c *net.IPConn
err error
)
v6 := address.IP.To4() == nil
if v6 {
c, err = net.DialIP("ip6:ipv6-icmp", nil, address)
} else {
c, err = net.DialIP("ip4:icmp", nil, address)
}
if err != nil {
return err
}
c.SetDeadline(time.Now().Add(timeout))
defer c.Close()
typ := icmpv4EchoRequest
if v6 {
typ = icmpv6EchoRequest
}
xid, xseq := os.Getpid()&0xffff, 1
wb, err := (&icmpMessage{
Type: typ, Code: 0,
Body: &icmpEcho{
ID: xid, Seq: xseq,
Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
},
}).Marshal()
if err != nil {
return err
}
if _, err = c.Write(wb); err != nil {
return err
}
var m *icmpMessage
rb := make([]byte, 20+len(wb))
for {
if _, err = c.Read(rb); err != nil {
return err
}
if !v6 {
rb = ipv4Payload(rb)
}
if m, err = parseICMPMessage(rb); err != nil {
return err
}
switch m.Type {
case icmpv4EchoRequest, icmpv6EchoRequest:
continue
}
break
}
return nil
}