FAQ
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.Buffer
buff.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]byte
     for {
     conn.ReadFromUDP(buf[:])
     conn.Write([]byte("OK"))
conn.Close()
}

It should go like this: client connects server responds with OK. I've added
the ReadFromUDP because otherwise it's CPU death by gopher :) This is my
first time writing an UDP client/server program.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Mikio Hara at Sep 12, 2014 at 2:55 pm

    On Fri, Sep 12, 2014 at 1:44 AM, wrote:

    conn, _ := net.DialUDP("udp", nil, &addr)
    var buff bytes.Buffer
    buff.ReadFrom(conn)
    the net package doesn't provide anything good to fight the leaky
    abstractions. you need to have a look at
    http://golang.org/pkg/bytes/#Buffer.ReadFrom.

    ps: read operation on udp socket is basically per-datagram basis, no EOF.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 12, 2014 at 3:38 pm
    So how do I read the whole data from the socket without creating a 64k
    slice? Is there some delimiter character I can use?
    On Friday, September 12, 2014 5:55:29 PM UTC+3, Mikio Hara wrote:

    On Fri, Sep 12, 2014 at 1:44 AM, <boghi...@gmail.com <javascript:>>
    wrote:
    conn, _ := net.DialUDP("udp", nil, &addr)
    var buff bytes.Buffer
    buff.ReadFrom(conn)
    the net package doesn't provide anything good to fight the leaky
    abstractions. you need to have a look at
    http://golang.org/pkg/bytes/#Buffer.ReadFrom.

    ps: read operation on udp socket is basically per-datagram basis, no EOF.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Sep 12, 2014 at 3:48 pm

    On Friday, September 12, 2014 11:38:17 AM UTC-4, boghi...@gmail.com wrote:
    So how do I read the whole data from the socket without creating a 64k
    slice? Is there some delimiter character I can use?

    You basically have to use a buffer that can handle the maximum packet size
    you might see. If you don't have an upper bound, having a single 64k buffer
    isn't really a big deal unless you're on some embedded system. There is a
    rather awkward mechanisms to get the incoming packet size, but I don't
    think there's any feasible way to do it in Go.

    http://stackoverflow.com/questions/24704680/read-data-from-a-udp-socket-with-an-unknown-length




    On Friday, September 12, 2014 5:55:29 PM UTC+3, Mikio Hara wrote:
    On Fri, Sep 12, 2014 at 1:44 AM, wrote:

    conn, _ := net.DialUDP("udp", nil, &addr)
    var buff bytes.Buffer
    buff.ReadFrom(conn)
    the net package doesn't provide anything good to fight the leaky
    abstractions. you need to have a look at
    http://golang.org/pkg/bytes/#Buffer.ReadFrom.

    ps: read operation on udp socket is basically per-datagram basis, no EOF.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 12, 2014 at 5:46 pm
    I still don't get why my code doesn't work. What have I done wrong? I have
    tried it with a []byte, net.ResolveUDPAddr. So far nothing is working.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Sep 12, 2014 at 6:07 pm

    On Thursday, September 11, 2014 12:44:49 PM UTC-4, boghi...@gmail.com wrote:
    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.Buffer
    buff.ReadFrom(conn)
    As Mikio said, UDP doesn't have an EOF, so you can't use ReadFrom. Just
    read into a []byte slice like usual.


    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]byte
    for {
    conn.ReadFromUDP(buf[:])
    You're also not checking the read length, or errors, anywhere. You *need*
    the bytes in order know where to slice buf.


    conn.Write([]byte("OK"))
    This is a listening connection. You can't send because there's no
    destination address (you would see this if you checked the error)

    conn.Close()
    You're closing the connection in your for loop, causing it to go into an
    infinite loop. Check errors again.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 12, 2014 at 6:14 pm
    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?
    On Friday, September 12, 2014 9:07:48 PM UTC+3, James Bardin wrote:



    On Thursday, September 11, 2014 12:44:49 PM UTC-4, boghi...@gmail.com
    wrote:
    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.Buffer
    buff.ReadFrom(conn)
    As Mikio said, UDP doesn't have an EOF, so you can't use ReadFrom. Just
    read into a []byte slice like usual.


    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]byte
    for {
    conn.ReadFromUDP(buf[:])
    You're also not checking the read length, or errors, anywhere. You *need*
    the bytes in order know where to slice buf.


    conn.Write([]byte("OK"))
    This is a listening connection. You can't send because there's no
    destination address (you would see this if you checked the error)

    conn.Close()
    You're closing the connection in your for loop, causing it to go into an
    infinite loop. Check errors again.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Sep 12, 2014 at 7:19 pm

    On Friday, September 12, 2014 2:13:58 PM UTC-4, boghi...@gmail.com wrote:
    I've rewritten the code, so everything is nice, except the write part.
    I don't understand, do you have a question about Write()?

    Also, do I need the ReadFromUDP before the loop, or inside it?
    If you're using a loop, I assume you want to read more than one datagram?
    If you only want to read once, then you don't need a loop.



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 13, 2014 at 2:30 pm
    I've updated the code, so server:
    package main

    import (
    "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]byte
         for {
         _, 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]byte
    n, _, _ := conn.ReadFromUDP(buff[0:])
    fmt.Println(string(buff[0:n]))

    still nothing works :(

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Sep 13, 2014 at 5:47 pm
    You still not Chow ng the returned errors.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Sep 13, 2014 at 9:24 pm
    Stop ignoring errors. They will likely tell you exactly what is wrong.
    On Saturday, September 13, 2014, wrote:

    I've updated the code, so server:
    package main

    import (
    "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]byte
    for {
    _, 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]byte
    n, _, _ := conn.ReadFromUDP(buff[0:])
    fmt.Println(string(buff[0:n]))

    still nothing works :(

    --
    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+unsubscribe@googlegroups.com
    <javascript:_e(%7B%7D,'cvml','golang-nuts%2bunsubscribe@googlegroups.com');>
    .
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mikio Hara at Sep 14, 2014 at 1:31 am

    On Sat, Sep 13, 2014 at 11:30 PM, wrote:

    still nothing works :(
    looks like your confusion is not related to the go standard library,
    it comes from the underlying stuff: network protools and kernel
    services that try to abstract the way of access to each protocol.
    fwiw, the net package holds tons of dumb test cases and perhaps it
    might be help you.

    PacketConn peers: http://golang.org/src/pkg/net/packetconn_test.go#L67
    Conn server and PacketConn client:
    http://golang.org/src/pkg/net/packetconn_test.go#L122

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Sep 14, 2014 at 3:03 am

    On Sunday, 14 September 2014 00:30:04 UTC+10, boghi...@gmail.com wrote:
    I've updated the code, so server:
    package main

    import (
    "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]byte
    for {
    _, nextAddr, _ := conn.ReadFromUDP(buf[0:])
    You've ignored the size of the number of bytes read into buf (it may not be
    512), and also the error.

    fmt.Println("yay")
    conn.WriteToUDP([]byte("OK"), nextAddr)
    }
    }

    client:

    addr, _ := net.ResolveUDPAddr("udp", ":2000")
    conn, _ := net.DialUDP("udp", nil, addr)
    var buff [512]byte
    n, _, _ := conn.ReadFromUDP(buff[0:])
    fmt.Println(string(buff[0:n]))

    still nothing works :(
    You need to check all the errors before we can offer you more constructive
    feedback.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jesse McNelis at Sep 14, 2014 at 6:51 am

    On Sun, Sep 14, 2014 at 12:30 AM, wrote:
    I've updated the code, so server:
    package main

    import (
    "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]byte
    for {
    _, nextAddr, _ := conn.ReadFromUDP(buf[0:])
    fmt.Println("yay")
    conn.WriteToUDP([]byte("OK"), nextAddr)
    }
    }
    Your server is waiting to receive data from the client.
    client:

    addr, _ := net.ResolveUDPAddr("udp", ":2000")
    conn, _ := net.DialUDP("udp", nil, addr)
    var buff [512]byte
    n, _, _ := conn.ReadFromUDP(buff[0:])
    fmt.Println(string(buff[0:n]))
    Your client is waiting to receive data from the server.

    still nothing works :(
    You have a deadlock.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 14, 2014 at 7:33 am
    So, I added WriteToUDP into the client code and it still doesn't work.
    On Sunday, September 14, 2014 9:51:47 AM UTC+3, Jesse McNelis wrote:

    On Sun, Sep 14, 2014 at 12:30 AM, <boghi...@gmail.com <javascript:>>
    wrote:
    I've updated the code, so server:
    package main

    import (
    "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]byte
    for {
    _, nextAddr, _ := conn.ReadFromUDP(buf[0:])
    fmt.Println("yay")
    conn.WriteToUDP([]byte("OK"), nextAddr)
    }
    }
    Your server is waiting to receive data from the client.
    client:

    addr, _ := net.ResolveUDPAddr("udp", ":2000")
    conn, _ := net.DialUDP("udp", nil, addr)
    var buff [512]byte
    n, _, _ := conn.ReadFromUDP(buff[0:])
    fmt.Println(string(buff[0:n]))
    Your client is waiting to receive data from the server.

    still nothing works :(
    You have a deadlock.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 14, 2014 at 6:44 am
    I checked the errors and guess what: no errors whatsoever. I've considered
    packet loss, but since it's localhost to localhost, i doubt it. The buffer
    is more that enough: the server doesn't even need to read anything and the
    client reads OK, how many bytes do you think are in OK?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Boghison22 at Sep 14, 2014 at 9:02 am
    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?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Sep 14, 2014 at 9:05 am
    UDP doesn't have a concept of end, packets must be read in full on the first try, packets may also arrive out of order or not at all. If you use UDP you need to deal with these eventualities.


    On 14 Sep 2014, at 19:02, boghison22@gmail.com wrote:

    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?
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 12, '14 at 1:09p
activeSep 14, '14 at 9:05a
posts18
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase