Ref http://lua-users.org/wiki/CopasExample
1. Macbook pro (OS X EI Captian 10.11.5)
2. lua 5.2.4
brew install lua
注意 : 下載 lua 5.3.3 for Mac
Download lua for Mac 5.3.3
http://luabinaries.sourceforge.net/download.html
3. 安裝copas
luarocks install copas
4. 安裝 socket
sudo luarocks install luasocket
5. 參考範例備妥 luaCopasEx3.lua
-- Ref http://lua-users.org/wiki/CopasExample
-- Test OK 2016/8/18
-- luaCopasEx3.lua
-- lua -E luaCopasEx3.lua
local socket = require("socket")
local copas = require("copas")
function handler(c, host, port)
local peer = host .. ":" .. port
print("connection from", peer)
c:send("Hello 中文試試看\r\n")
print("data from", peer, (c:receive"*l"))
end
copas.addserver(assert(socket.bind("*",2220)),
function(c) return handler(copas.wrap(c), c:getpeername()) end
)
copas.loop()
6. 結果
沒有留言:
張貼留言