FAQ
Hi Guys,
i want to write some code for UDP server to prevent it from receiveing
buffer overflows... i have run across this example:

https://play.golang.org/p/3h1MMS90zI

b := bytes.NewBuffer(nil)
go io.Copy(b, c)

Basically it copies data received from socket to a new buffer, the question
if how much data it'll read before stopping to "take" new input, eg. it'll
copy data from c to b indefinitely, untill b fills up, wich is max size of
data that can stay in b before the read operation on c will block?

Or is it preferred to use this code:
sock, _ := net.ListenUDP("udp", addr)
sock.SetReadBuffer(1048576)

Thanks!

--
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

  • Jesse McNelis at Mar 18, 2015 at 2:24 am

    On Wed, Mar 18, 2015 at 12:24 PM, Slawomir Pryczek wrote:
    Hi Guys,
    i want to write some code for UDP server to prevent it from receiveing
    buffer overflows... i have run across this example:

    https://play.golang.org/p/3h1MMS90zI

    b := bytes.NewBuffer(nil)
    go io.Copy(b, c)
    It looks like you probably want to use
    http://golang.org/pkg/bufio/#NewReader instead.
    It will handle the buffering for you.

    eg. https://play.golang.org/p/alhQ6wB1KE
    Basically it copies data received from socket to a new buffer, the question
    if how much data it'll read before stopping to "take" new input, eg. it'll
    copy data from c to b indefinitely, untill b fills up, wich is max size of
    data that can stay in b before the read operation on c will block?
    You can use http://golang.org/pkg/bufio/#NewReaderSize so you can pick
    the size of the buffer you want.
    Or is it preferred to use this code:
    sock, _ := net.ListenUDP("udp", addr)
    sock.SetReadBuffer(1048576)
    --
    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.
  • Slawomir Pryczek at Mar 18, 2015 at 2:07 pm
    Thanks!

    W dniu środa, 18 marca 2015 03:24:48 UTC+1 użytkownik Jesse McNelis napisał:
    On Wed, Mar 18, 2015 at 12:24 PM, Slawomir Pryczek <slawe...@gmail.com
    <javascript:>> wrote:
    Hi Guys,
    i want to write some code for UDP server to prevent it from receiveing
    buffer overflows... i have run across this example:

    https://play.golang.org/p/3h1MMS90zI

    b := bytes.NewBuffer(nil)
    go io.Copy(b, c)
    It looks like you probably want to use
    http://golang.org/pkg/bufio/#NewReader instead.
    It will handle the buffering for you.

    eg. https://play.golang.org/p/alhQ6wB1KE
    Basically it copies data received from socket to a new buffer, the question
    if how much data it'll read before stopping to "take" new input, eg. it'll
    copy data from c to b indefinitely, untill b fills up, wich is max size of
    data that can stay in b before the read operation on c will block?
    You can use http://golang.org/pkg/bufio/#NewReaderSize so you can pick
    the size of the buffer you want.
    Or is it preferred to use this code:
    sock, _ := net.ListenUDP("udp", addr)
    sock.SetReadBuffer(1048576)
    --
    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 Mar 18, 2015 at 2:15 pm
    A UDP socket is *not* a stream!

    I just want to point that out, in case you're not aware that reading a
    partial datagram from a socket *discards* the rest of the data completely.
    You can't really use a buffered reader on a UDP socket. If it issues a
    small read to fill up the buffer, you will lose data.

    On Wednesday, March 18, 2015 at 9:58:46 AM UTC-4, Slawomir Pryczek wrote:

    Thanks!

    W dniu środa, 18 marca 2015 03:24:48 UTC+1 użytkownik Jesse McNelis
    napisał:
    i want to write some code for UDP server to prevent it from receiveing
    buffer overflows... i have run across this example:
    What buffer are you trying to prevent an overflow? You read each datagram
    independently into a predetermined size buffer. Only the amount you request
    is ever available.

    Or is it preferred to use this code:
    sock, _ := net.ListenUDP("udp", addr)
    sock.SetReadBuffer(1048576)
    SetReadBuffer sets the operating system buffer for unread packets, up the
    the systems rmem_max setting. This has nothing to do with your programs
    buffering.

    --
    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.
  • Slawomir Pryczek at Mar 18, 2015 at 4:00 pm
    Hey James, ok makes sense ... i was actually thinking about just reading to
    65k []byte and then passing it to another thread using channel, sounds like
    more "natural" solution, what you think...

    For SetReadBuffer im aware, yes, i just wanted to do complete solution to
    prevent dropped packets.



    W dniu środa, 18 marca 2015 15:15:37 UTC+1 użytkownik James Bardin napisał:
    A UDP socket is *not* a stream!

    I just want to point that out, in case you're not aware that reading a
    partial datagram from a socket *discards* the rest of the data completely.
    You can't really use a buffered reader on a UDP socket. If it issues a
    small read to fill up the buffer, you will lose data.

    On Wednesday, March 18, 2015 at 9:58:46 AM UTC-4, Slawomir Pryczek wrote:

    Thanks!

    W dniu środa, 18 marca 2015 03:24:48 UTC+1 użytkownik Jesse McNelis
    napisał:
    i want to write some code for UDP server to prevent it from receiveing
    buffer overflows... i have run across this example:
    What buffer are you trying to prevent an overflow? You read each datagram
    independently into a predetermined size buffer. Only the amount you request
    is ever available.

    Or is it preferred to use this code:
    sock, _ := net.ListenUDP("udp", addr)
    sock.SetReadBuffer(1048576)
    SetReadBuffer sets the operating system buffer for unread packets, up the
    the systems rmem_max setting. This has nothing to do with your programs
    buffering.
    --
    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 Mar 18, 2015 at 4:42 pm

    On Wed, Mar 18, 2015 at 12:00 PM, Slawomir Pryczek wrote:

    Hey James, ok makes sense ... i was actually thinking about just reading
    to 65k []byte and then passing it to another thread using channel, sounds
    like more "natural" solution, what you think...
    That's one way of doing it, but if you're processing packets fast enough to
    worry about dropping some, creating a 65k slice for each packet is going to
    be really hard on the sever. If you want to process the UDP packets
    concurrently, you need to reuse the slices and manage your copies
    carefully.

    You also probably don't need a full 65k buffer for each datagram. You
    should generally know what packets size to expect, and that will give you
    the buffer size you need. Datagrams will never be combined, so if the
    application never sends out more than a 2000B message for example, that
    will be an adequate buffer size on the server. It's very common to try keep
    datagram size small enough to fit in single packets without fragmentation,
    so 1472 bytes.

    --
    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
postedMar 18, '15 at 1:24a
activeMar 18, '15 at 4:42p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase