Getting ICMP port unreachable error for UDP connection

186 views
Skip to first unread message

Ihsan Junaidi Ibrahim

unread,
Nov 12, 2013, 10:07:17 AM11/12/13
to golang-nuts
Hi,

I'm trying to implement a UDP ping mechanism but my code currently returns io.timeout.

Since no packets ever reach the UDP port except for ICMP replies, hence net.Error returned io.timeout

Is there a way I can obtain the reply or at least test for a different error condition that reflect unreachable errors?

--
var con *net.UDPConn

if con, err = net.ListenUDP(udp, laddr); err != nil {
return rtt, tgt, err.(net.Error)
}
defer con.Close()

con.SetDeadline(t.Add(100 * time.Millisecond))

if _, err = con.WriteToUDP([]byte("PING"), addr); err != nil {
return rtt, tgt, err.(net.Error)
}

var buf = make([]byte, 4)

if _, _, err = con.ReadFromUDP(buf); err != nil {
return rtt, tgt, err.(net.Error)
}


Reply all
Reply to author
Forward
0 new messages