News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
How to advertise?
Author Message
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #1
RE: How to advertise?

This is the coroutine download function from the 5.1 "Programming in Lua" book. It gives error "attempt to call global 'receive' (a nil value)".

http=require('socket.http')
function download (host, file)
local c = assert(socket.connect(host, 80))
local count = 0 -- counts number of bytes read
c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
while true do
local s, status = receive©
print (status, string.len(s))
count = count + string.len(s)
if status == "closed" then break end
end
c:close()
print(file, count)
end
host = "www.w3.org"
download(host, "/TR/html401/html40.txt")

This is the entire coroutine example. It just hangs up.

function download (host, file)
local c = assert(socket.connect(host, 80))
local count = 0 -- counts number of bytes read
c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
while true do
local s, status = receive©
print (status, string.len(s))
count = count + string.len(s)
if status == "closed" then break end
end
c:close()
print(file, count)
end
threads = {} -- list of all live threads
function get (host, file)
-- create coroutine
local co = coroutine.create(function ()
download(host, file)
end)
-- insert it in the list
table.insert(threads, co)
end
function receive (connection)
connection:timeout(0) -- do not block
local s, status = connection:receive(2^10)
if status == "timeout" then
coroutine.yield(connection)
end
return s, status
end
function dispatcher ()
while true do
local n = table.getn(threads)
if n == 0 then break end -- no more threads to run
local connections = {}
for i=1,n do
local status, res = coroutine.resume(threads[i])
print 'howdy'
if not res then -- thread finished its task?
table.remove(threads, i)
break
else -- timeout
table.insert(connections, res)
end
end
if table.getn(connections) == n then
socket.select(connections)
end
end
end
host = "www.w3.org"
download(host, "/TR/html401/html40.txt")
get(host, "/TR/html401/html40.txt")
get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
get(host,"/TR/REC-html32.html")
get(host,"/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")
dispatcher() -- main loop

01-25-2008 06:27 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
How to advertise? - mikshaw - 01-21-2008, 12:42 PM
RE: How to advertise? - iGame3D - 01-21-2008, 05:31 PM
RE: How to advertise? - JohnMurga - 01-21-2008, 05:51 PM
RE: How to advertise? - asafp - 01-23-2008, 04:19 AM
RE: How to advertise? - JohnMurga - 01-24-2008, 07:02 PM
RE: How to advertise? - iGame3D - 01-23-2008, 08:45 AM
RE: How to advertise? - asafp - 01-25-2008, 06:12 AM
RE: How to advertise? - asafp - 01-25-2008 06:27 AM
RE: How to advertise? - asafp - 01-25-2008, 06:52 AM
RE: How to advertise? - mikshaw - 01-25-2008, 07:33 AM
RE: How to advertise? - asafp - 01-25-2008, 08:42 AM
RE: How to advertise? - mikshaw - 01-25-2008, 09:41 AM
RE: How to advertise? - JohnMurga - 01-26-2008, 02:21 AM
RE: How to advertise? - JohnMurga - 01-26-2008, 07:03 AM
RE: How to advertise? - mmburke - 01-31-2008, 08:23 AM
RE: How to advertise? - mikshaw - 01-31-2008, 11:11 AM
RE: How to advertise? - iGame3D - 01-31-2008, 08:30 PM
RE: How to advertise? - mmburke - 02-01-2008, 03:04 AM
RE: How to advertise? - asafp - 02-01-2008, 04:06 AM
RE: How to advertise? - JohnMurga - 02-01-2008, 04:18 AM
RE: How to advertise? - JohnMurga - 02-01-2008, 04:08 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: