icmp.ListenPacket()
creates *icmp.PacketConn
- in other words, the listener. You create listeners in an endless loop! Since you donβt even close them, your program will start complaining too many open files
faster than you can say βpingβ.
Here is an example of a working listener
package main
import (
"golang.org/x/net/icmp"
"log"
)
func main() {
conn, err := icmp.ListenPacket("ip4:icmp", "192.168.0.12")
if err != nil {
log.Fatal(err)
}
for {
var msg []byte
length, sourceIP, err := conn.ReadFrom(msg)
if err != nil {
log.Println(err)
continue
}
log.Printf("message = '%s', length = %d, source-ip = %s", string(msg), length, sourceIP)
}
_ = conn.Close()
}
This gives:
2015/10/26 10:35:00 message = '', length = 0, source-ip = 192.168.0.7
2015/10/26 10:35:00 message = '', length = 0, source-ip = 192.168.0.25
2015/10/26 10:35:01 message = '', length = 0, source-ip = 192.168.0.7
2015/10/26 10:35:01 message = '', length = 0, source-ip = 192.168.0.25
2015/10/26 10:35:02 message = '', length = 0, source-ip = 192.168.0.7
2015/10/26 10:35:02 message = '', length = 0, source-ip = 192.168.0.25
.
, .