FAQ
I've been having trouble wrapping my head around around a problem I've been
having with the standard net package.

I want to read n bytes at a time of a socket. In this case 10 bytes of a
socket to google's webserver.

Full code: http://play.golang.org/p/zAQsO1x9rW

Running that I get:
Read: [72 84 84 80 47 49 46 48 32 50]
Read: [72 84 84 80 47 49 46 48 32 50]
Read: [72 84 84 80 47 49 46 48 32 50]
Read: [72 84 84 80 47 49 46 48 32 50]
Read: [72 84 84 80 47 49 46 48 32 50]
Read: [72 84 84 80 47 49 46 48 32 50]
...
Continuing on forever.

It appears buf is not changing for each iteration of the loop. The first 10
bytes of the server's response stays in the slice through each iteration of
the loop.

I've tried using bufio package to wrap it but the problem remains the same.

--

Search Discussions

  • Dave Cheney at Nov 15, 2012 at 7:15 am
    You are ignoring the number of bytes returned from Read

    http://golang.org/pkg/io/#Reader

    You should reslice buf using the number of bytes actually read.

    Cheers

    Dave
    On Thu, Nov 15, 2012 at 6:10 PM, Hunter F wrote:
    I've been having trouble wrapping my head around around a problem I've been
    having with the standard net package.

    I want to read n bytes at a time of a socket. In this case 10 bytes of a
    socket to google's webserver.

    Full code: http://play.golang.org/p/zAQsO1x9rW

    Running that I get:
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    ...
    Continuing on forever.

    It appears buf is not changing for each iteration of the loop. The first 10
    bytes of the server's response stays in the slice through each iteration of
    the loop.

    I've tried using bufio package to wrap it but the problem remains the same.

    --
    --
  • Volker Dobler at Nov 15, 2012 at 7:19 am
    Not sure if this is the issue, but from http://golang.org/pkg/io/#Reader
    "It returns the number of bytes read (0 <= n <= len(p)) [...]
    Read conventionally returns what is available instead of waiting for more."

    It seems as if your loop reads the first 10 bytes properly and then reads
    only 0 bytes (thus not changing buf). Check the number of bytes read
    and print only those.

    Volker

    Am Donnerstag, 15. November 2012 08:10:43 UTC+1 schrieb Hunter F:
    I've been having trouble wrapping my head around around a
    problem I've been having with the standard net package.

    I want to read n bytes at a time of a socket. In this case 10 bytes of a
    socket to google's webserver.

    Full code: http://play.golang.org/p/zAQsO1x9rW

    Running that I get:
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    ...
    Continuing on forever.

    It appears buf is not changing for each iteration of the loop. The first
    10 bytes of the server's response stays in the slice through each iteration
    of the loop.

    I've tried using bufio package to wrap it but the problem remains the same.
    --
  • DisposaBoy at Nov 15, 2012 at 7:33 am
    two issues. one has already been given: you should not ignore the n return which tells you how much data was actually read. and the source of your infinite old data is that you're only doing one read. that is the pre-condition read . after that the pre-condition is never executed again and you there have an infinite loop on a variable that never changes

    --
  • Ethan Burns at Nov 15, 2012 at 1:09 pm
    This for loop does one read, then loops as long as err == nil; it never
    reads again.
    for _, err := conn.Read(buf); err == nil; {
    fmt.Println("Read:", buf)
    }

    You probably wanted this:
    for _, err := conn.Read(buf); err == nil; _, err := conn.Read(buf){
    fmt.Println("Read:", buf)
    }

    See this thread for a bit more
    info: https://groups.google.com/d/topic/golang-nuts/QfKNn6ljc5Q/discussion

    Best,
    Ethan
    On Thursday, November 15, 2012 2:10:43 AM UTC-5, Hunter F wrote:

    I've been having trouble wrapping my head around around a
    problem I've been having with the standard net package.

    I want to read n bytes at a time of a socket. In this case 10 bytes of a
    socket to google's webserver.

    Full code: http://play.golang.org/p/zAQsO1x9rW

    Running that I get:
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    ...
    Continuing on forever.

    It appears buf is not changing for each iteration of the loop. The first
    10 bytes of the server's response stays in the slice through each iteration
    of the loop.

    I've tried using bufio package to wrap it but the problem remains the same.
    --
  • Ethan Burns at Nov 15, 2012 at 1:10 pm
    BTW, as others have said, you should always check the number of bytes read.

    Best,
    Ethan
    On Thursday, November 15, 2012 8:09:41 AM UTC-5, Ethan Burns wrote:

    This for loop does one read, then loops as long as err == nil; it never
    reads again.
    for _, err := conn.Read(buf); err == nil; {
    fmt.Println("Read:", buf)
    }

    You probably wanted this:
    for _, err := conn.Read(buf); err == nil; _, err := conn.Read(buf){
    fmt.Println("Read:", buf)
    }

    See this thread for a bit more info:
    https://groups.google.com/d/topic/golang-nuts/QfKNn6ljc5Q/discussion

    Best,
    Ethan
    On Thursday, November 15, 2012 2:10:43 AM UTC-5, Hunter F wrote:

    I've been having trouble wrapping my head around around a
    problem I've been having with the standard net package.

    I want to read n bytes at a time of a socket. In this case 10 bytes of a
    socket to google's webserver.

    Full code: http://play.golang.org/p/zAQsO1x9rW

    Running that I get:
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    Read: [72 84 84 80 47 49 46 48 32 50]
    ...
    Continuing on forever.

    It appears buf is not changing for each iteration of the loop. The first
    10 bytes of the server's response stays in the slice through each iteration
    of the loop.

    I've tried using bufio package to wrap it but the problem remains the
    same.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 15, '12 at 7:10a
activeNov 15, '12 at 1:10p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase