You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"os"; "net"; "fmt"
)
func main() {
udp := os.Args[1];
if udp != "" {
udpaddr,ok := net.ResolveUDPAddr(udp);
if ok == nil {
conn, ok := net.ListenUDP("udp", udpaddr);
conn.SetReadTimeout(10 * 1000*1000*1000);
if ok == nil {
var b [1024]byte;
for {
nb,ok := conn.Read(&b);
fmt.Printf("%d = |%s|\n", nb,ok);
switch nb {
case 0:
fmt.Println("expected =
",os.EAGAIN);
case -1:
fmt.Println(ok);
os.Exit(1);
default:
fmt.Println(b[0:nb]);
}
}
}
}
}
}
With no data sent to udp:1234
$ ./udp 0.0.0.0:1234
0 = |read udp:[::]:1234->: resource temporarily unavailable|
expected = resource temporarily unavailable
0 = |read udp:[::]:1234->: resource temporarily unavailable|
expected = resource temporarily unavailable
Linux, Ubuntu 9.10, AMD64
changeset: 4052:b72ec78019b4
tag: tip
In addition, if I change conn.Read to conn.ReadFrom, I get an os.Error:
$ ./udp 0.0.0.0:1234
-1 = |resource temporarily unavailable|
resource temporarily unavailable
And if, like issue #159, I instead use net.ListenPacket("udp",
"0.0.0.0:1234"), then os.EAGAIN fires continually, regardless of the
timeout.
The text was updated successfully, but these errors were encountered:
OpError is an implementation of os.Error.
It gives additional context. The way to look for EAGAIN is:
if e, ok := err.(*net.OpError); ok && e.Error == os.EAGAIN {
your code here
}
See http://golang.org/doc/effective_go.html#errors
Also, in Go, Read doesn't overload the number of bytes returned (nb)
with an error signal; you'll never see -1, always 0.
The issue with ReadFrom is a different bug,
https://golang.org/issue/153
mikioh
changed the title
UDP Read returns what appears to be OpError, not os.Error
net: UDP Read returns what appears to be OpError, not os.Error
Aug 5, 2015
by jonathan.r.hudson:
The text was updated successfully, but these errors were encountered: