Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: UDP Read returns what appears to be OpError, not os.Error #200

Closed
gopherbot opened this issue Nov 15, 2009 · 1 comment
Closed

net: UDP Read returns what appears to be OpError, not os.Error #200

gopherbot opened this issue Nov 15, 2009 · 1 comment

Comments

@gopherbot
Copy link

by jonathan.r.hudson:

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.
@rsc
Copy link
Contributor

rsc commented Nov 15, 2009

Comment 1:

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

Owner changed to r...@golang.org.

Status changed to WontFix.

@mikioh 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
@golang golang locked and limited conversation to collaborators Aug 5, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants