{"action":"opened","issue":{"url":"https://api.github.com/repos/golang/go/issues/29451","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/29451/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/29451/comments","events_url":"https://api.github.com/repos/golang/go/issues/29451/events","html_url":"https://github.com/golang/go/issues/29451","id":394773424,"node_id":"MDU6SXNzdWUzOTQ3NzM0MjQ=","number":29451,"title":"net: DialUDP unexpected behavior when laddr not nil","user":{"login":"Astone-Chou","id":11812510,"node_id":"MDQ6VXNlcjExODEyNTEw","avatar_url":"https://avatars3.githubusercontent.com/u/11812510?v=4","gravatar_id":"","url":"https://api.github.com/users/Astone-Chou","html_url":"https://github.com/Astone-Chou","followers_url":"https://api.github.com/users/Astone-Chou/followers","following_url":"https://api.github.com/users/Astone-Chou/following{/other_user}","gists_url":"https://api.github.com/users/Astone-Chou/gists{/gist_id}","starred_url":"https://api.github.com/users/Astone-Chou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Astone-Chou/subscriptions","organizations_url":"https://api.github.com/users/Astone-Chou/orgs","repos_url":"https://api.github.com/users/Astone-Chou/repos","events_url":"https://api.github.com/users/Astone-Chou/events{/privacy}","received_events_url":"https://api.github.com/users/Astone-Chou/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-12-29T08:17:36Z","updated_at":"2018-12-29T08:17:36Z","closed_at":null,"author_association":"NONE","body":"<!-- Please answer these questions before submitting your issue. Thanks! -->\r\n\r\n### What version of Go are you using (`go version`)?\r\n\r\n<pre>\r\ngo version go1.9 linux/amd64\r\n</pre>\r\n\r\n### Does this issue reproduce with the latest release?\r\nYes\r\n\r\n\r\n### What operating system and processor architecture are you using (`go env`)?\r\n\r\n<details><summary><code>go env</code> Output</summary><br><pre>\r\nGOARCH=\"amd64\"\r\nGOBIN=\"/root/Astone/git/bin\"\r\nGOEXE=\"\"\r\nGOHOSTARCH=\"amd64\"\r\nGOHOSTOS=\"linux\"\r\nGOOS=\"linux\"\r\nGOPATH=\"/root/Astone/.go:/root/Astone/git\"\r\nGORACE=\"\"\r\nGOROOT=\"/usr/local/go\"\r\nGOTOOLDIR=\"/usr/local/go/pkg/tool/linux_amd64\"\r\nGCCGO=\"gccgo\"\r\nCC=\"gcc\"\r\nGOGCCFLAGS=\"-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build412440145=/tmp/go-build -gno-record-gcc-switches\"\r\nCXX=\"g++\"\r\nCGO_ENABLED=\"1\"\r\nCGO_CFLAGS=\"-g -O2\"\r\nCGO_CPPFLAGS=\"\"\r\nCGO_CXXFLAGS=\"-g -O2\"\r\nCGO_FFLAGS=\"-g -O2\"\r\nCGO_LDFLAGS=\"-g -O2\"\r\nPKG_CONFIG=\"pkg-config\"\r\n</pre></details>\r\n\r\n### What did you do?\r\n\r\n<!--\r\nIf possible, provide a recipe for reproducing the error.\r\nA complete runnable program is good.\r\nA link on play.golang.org is best.\r\n-->\r\n\r\nI wanted to implement a UDP accept.\r\n\r\n```\r\npackage main\r\n\r\nimport (\r\n\t\"flag\"\r\n\t\"fmt\"\r\n\t\"net\"\r\n\t\"time\"\r\n)\r\n\r\nvar (\r\n\tc = flag.Bool(\"c\", false, \"client\")\r\n\ts = flag.Bool(\"s\", false, \"server\")\r\n)\r\n\r\nvar (\r\n\ttimes = 10\r\n\tport  = \"19880\"\r\n)\r\n\r\nfunc C() {\r\n\taddr, err := net.ResolveUDPAddr(\"udp\", \"127.0.0.1:\"+port)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tvar clt net.Conn\r\n\tclt, err = net.DialUDP(\"udp\", nil, addr)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tfor i := 0; i < times; i++ {\r\n\t\tdata := fmt.Sprintf(\"%d:%s\\n\", i, \"Hello\")\r\n\t\tfmt.Print(\"Send:\", data)\r\n\t\ttime.Sleep(time.Second)\r\n\t\t_, err := clt.Write([]byte(data))\r\n\t\tif err != nil {\r\n\t\t\tfmt.Println(\"ERROR:\", err)\r\n\t\t}\r\n\t}\r\n}\r\n\r\nfunc S() {\r\n\tladdr, err := net.ResolveUDPAddr(\"udp\", \":\"+port)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tsvr, err := net.ListenUDP(\"udp\", laddr)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tfor {\r\n\t\tc, err := accept(svr)\r\n\t\tif err != nil {\r\n\t\t\tpanic(err)\r\n\t\t}\r\n\t\tgo serve(c)\r\n\t\tfmt.Println(\"Accept Success\", c.RemoteAddr())\r\n\t}\r\n}\r\n\r\nfunc accept(svr *net.UDPConn) (*net.UDPConn, error) {\r\n\tvar buffer [1024]byte\r\n\t_, raddr, err := svr.ReadFromUDP(buffer[:])\r\n\tif err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\r\n\t// auth\r\n\t// if n != \"SYN\" {\r\n\t//\treturn errors.New(\"invalid\")\r\n\t// }\r\n\r\n\treturn net.DialUDP(\"udp\", svr.LocalAddr().(*net.UDPAddr), raddr)\r\n}\r\n\r\nfunc serve(con *net.UDPConn) {\r\n\tvar buffer [1024]byte\r\n\tfor {\r\n\t\tn, err := con.Read(buffer[:])\r\n\t\tif err != nil {\r\n\t\t\tfmt.Println(err)\r\n\t\t\treturn\r\n\t\t}\r\n\t\tfmt.Println(string(buffer[:n]))\r\n\t}\r\n}\r\n\r\nfunc main() {\r\n\tflag.Parse()\r\n\tswitch {\r\n\tcase *c:\r\n\t\tC()\r\n\t\treturn\r\n\tcase *s:\r\n\t\tS()\r\n\t\treturn\r\n\t}\r\n}\r\n```\r\nwhen i start server and client i got an panic.\r\n```\r\npanic: dial udp [::]:19880->127.0.0.1:35398: bind: address already in use\r\n```\r\nI think it because of missing `SO_REUSEADDR` , so i modify `net/sock_posix.go` call `setDefaultListenerSockopts` in [listenDatagram](https://github.com/golang/go/blob/master/src/net/sock_posix.go#L216) and [dial](https://github.com/golang/go/blob/master/src/net/sock_posix.go#L138);\r\n\r\n```\r\n if err := setDefaultListenerSockopts(fd.pfd.Sysfd); err != nil {\r\n         return err\r\n }\r\n```\r\n\r\nIt's seems working well. I can implement the udp accept now.\r\n\r\n### What did you expect to see?\r\n\r\n\r\n\r\n### What did you see instead?\r\n\r\n"}}
{"actor":{"display_login":"Astone-Chou"}}