So how do I read the whole data from the socket without creating a 64k slice? Is there some delimiter character I can use?
So, I need to write an UDP Server/Client in Go. So far it's either been nothing or 100% CPU hogging resulting in death. Here's part of my client code:addr := net.UDPAddr{Port: 2000,IP: net.ParseIP("127.0.0.1"),}conn, _ := net.DialUDP("udp", nil, &addr)var buff bytes.Bufferbuff.ReadFrom(conn)
fmt.Println(string(buff.Bytes()))and the server:addr := net.UDPAddr{Port: 2000,IP: net.ParseIP("127.0.0.1"),}conn, err := net.ListenUDP("udp", &addr)
defer conn.Close()if err != nil {panic(err)}var buf [1024]bytefor {conn.ReadFromUDP(buf[:])
conn.Write([]byte("OK"))
conn.Close()
I've rewritten the code, so everything is nice, except the write part.
Also, do I need the ReadFromUDP before the loop, or inside it?
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oYlZaUnWdIU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I've updated the code, so server:package mainimport ("net""fmt")func main() {addr, _ := net.ResolveUDPAddr("udp", ":2000")conn, err := net.ListenUDP("udp", addr)defer conn.Close()if err != nil {panic(err)}var buf [512]bytefor {_, nextAddr, _ := conn.ReadFromUDP(buf[0:])
fmt.Println("yay")conn.WriteToUDP([]byte("OK"), nextAddr)}}client:addr, _ := net.ResolveUDPAddr("udp", ":2000")conn, _ := net.DialUDP("udp", nil, addr)var buff [512]byten, _, _ := conn.ReadFromUDP(buff[0:])fmt.Println(string(buff[0:n]))still nothing works :(
OK, I've fixed the client and everything works finally. I replaced DialUDP with Dial, WriteToUDP with Write and ReadFromUDP to Read. Can anybody tell me how I can read the data till the end from read instead of having a buffer?
--