我希望在客户端/服务器应用程序中实现UDP ping,客户端向任何服务器的临时端口发送UDP数据包,以尝试获取ICMP端口不可达回复。
我有以下代码。ReadFromUDP()从套接字返回错误= nil和0个字节读取。
问题是,我如何从服务器读取特定的端口不可达的ICMP回复?
conn, _ := net.ListenUDP("udp4", src)
defer conn.Close()
t := time.Now()
conn.SetDeadline(t.Add(100 * time.Millisecond))
conn.SetReadDeadline(t.Add(250 * time.Millisecond))
w, e := conn.WriteTo([]byte("PING"), dst)
if e != nil {
return nil, errors.New("Failed to send UDP4 ping request")
}
r, _, e := conn.ReadFromUDP(b4)
if e != nil {
return nil, errors.New("Failed to read UDP4 response packets")
}