From: Jeremy Bopp Date: 2010-12-08T08:36:16+09:00 Subject: Re: HTTP POST request --> Ruby server On 12/7/2010 4:36 PM, Chananya Freiman wrote: > Jeremy Bopp wrote in post #967039: >> On 12/7/2010 2:48 PM, Chananya Freiman wrote: >>> >>> str = "" >>> (1..560).each { |a| str += peer.readchar } >>> print str >> >> peer.readchar is blocking because there is no further data available but >> the stream is still open. >> >> Are you certain that your two examples both accurately reflect the code >> you're actually using? In both cases you are creating an inclusive >> range over which to iterate, and both ranges go from 1 to 560. As a >> result, your loops will run 560 times, so if your data is only 559 bytes >> long, you will run peer.readchar one more time than you have bytes >> available. In that case, both loops should block at the peer.readchar >> call in the 560th iteration. >> >> -Jeremy > > Yes that's what should happen, but the first one didn't block for some > reason. > > Anyway, the problem is solved now. As long as there is a question about why there is a difference in behavior in your code samples, there is a good chance that you've simply masked a bug that will bite you later. You should probably flag this code as needing attention at the very least unless this is a throwaway project. > I did try using readbytes(length) where length is the Content-Length > value, but it blocks wheres > > (length.to_i).times do > data += peer.readchar > end > > doesn't. Try using read(length) instead; although, readbytes(length) should work the same in this case. What you're doing is highly inefficient since you're creating a new string for every byte of data you read from your POST. If you start POSTing nontrivial amounts of data to your server, your solution will cause you problems. -Jeremy