[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Timeout on stdin
- From: "Xu Wang" <xu4wang@...>
- Date: Wed, 12 Nov 2008 10:35:44 +0800
Hi Jim,
I was searching for a solution but failed weeks ago. I workaround this by using LuaTask(threads).
One thread using Luasocket in asychronized mode and read it's queue when no data from the network, another thread reading the lua io.
Here is a small demo for the idea
----------------------------------------------------------------------------main.lua-------------------------------------------
local socket = require("socket")
require"task"
local tsk = task.create( "cli_get_input.lua" )
host = host or "localhost"
port = port or 9999
if arg then
host = arg[1] or host
port = arg[2] or port
end
host = socket.dns.toip(host)
tcp = assert(socket.tcp())
assert(tcp:setpeername(host, port))
print("Using remote host '" ..host.. "' and port " .. port .. "...")
tcp:settimeout(0.1)
while 1 do
--receive stdio input from another task
line,flag,rc=task.receive(10)
if rc==0 then
tcp:send(line.."\n")
end
dgram = tcp:receive()
if dgram then
print(dgram)
end
end
-----------------------------------------------------cli_get_input.lua-----------------------------------------
print"CLI running..."
while 1 do
line=io.read()
task.post(1,line,0)
end
Regards,
Xu