I tried to build a udp server with go and find that method ReadFromUDP of UDPConn does not block the thread. I google that there are some people facing this problem and no solution found.
my system is osx 10.8.2, go version is 1.1.1
code list bellow
addr, _ := net.ResolveUDPAddr("udp", "localhost:10234")
conn, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println(err.Error())
return
}
defer conn.Close()
var buf []byte
for {
n, remote_addr, _ := conn.ReadFromUDP(buf)
fmt.Println("from", remote_addr,"got message:", string(buf[:n]))
}
I got a list of: from <nil> got message:
I believe there's something broken in OSX. Your code, modified only slightly to not ignore errors etc., runs just fine on Linux.
I updated my system to osx 10.8.4 codes above does not work right.
i compare udp server code with mine.
and change var buf []byte
with buf := make([]byte, 1024)
it works well now.