Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
net package usage
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Hunter F  
View profile  
 More options Nov 15 2012, 2:10 am
From: Hunter F <h.g.f...@gmail.com>
Date: Wed, 14 Nov 2012 23:10:43 -0800 (PST)
Local: Thurs, Nov 15 2012 2:10 am
Subject: net package usage

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Cheney  
View profile  
 More options Nov 15 2012, 2:15 am
From: Dave Cheney <d...@cheney.net>
Date: Thu, 15 Nov 2012 18:15:23 +1100
Local: Thurs, Nov 15 2012 2:15 am
Subject: Re: [go-nuts] net package usage
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Volker Dobler  
View profile  
 More options Nov 15 2012, 2:19 am
From: Volker Dobler <dr.volker.dob...@gmail.com>
Date: Wed, 14 Nov 2012 23:19:19 -0800 (PST)
Local: Thurs, Nov 15 2012 2:19 am
Subject: Re: net package usage

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DisposaBoy  
View profile  
 More options Nov 15 2012, 2:33 am
From: DisposaBoy <disposa...@dby.me>
Date: Wed, 14 Nov 2012 23:33:51 -0800 (PST)
Local: Thurs, Nov 15 2012 2:33 am
Subject: net package usage

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Burns  
View profile  
 More options Nov 15 2012, 8:09 am
From: Ethan Burns <burns.et...@gmail.com>
Date: Thu, 15 Nov 2012 05:09:40 -0800 (PST)
Local: Thurs, Nov 15 2012 8:09 am
Subject: Re: net package usage

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Burns  
View profile  
 More options Nov 15 2012, 8:10 am
From: Ethan Burns <burns.et...@gmail.com>
Date: Thu, 15 Nov 2012 05:10:16 -0800 (PST)
Local: Thurs, Nov 15 2012 8:10 am
Subject: Re: net package usage

BTW, as others have said, you should always check the number of bytes read.

Best,
Ethan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »