目錄結構 LuaSocket 是 Lua 的網絡模塊庫,它可以很方便地提供 TCP、UDP、DNS、FTP、HTTP、SMTP、MIME 等多種網絡協議的訪問操作。 它由兩部分組成:一部分是用 C 寫的核心,提供對 TCP 和 UDP 傳輸層的訪問支持。另外一部分是用 Lua 寫的,負責應用功能的網絡接口處理。 一、安裝LuaSocket 下面介紹兩種安裝方法 第一種方法:如果你有安裝了 Lua 模塊的安裝和部署工具 LuaRocks,那么一條指令就能安裝部署好 LuaSocket: # luarocks install luasocket 第二種方法:如果沒安裝有 LuaRocks,也可以源碼安裝。 先把 LuaRocks 下載下來,當前可用的版本是 luasocket-3.0-rc1(luasocket的源碼有托管在Github.com): # git clone https://github.com/diegonehab/luasocket.git 把源碼clone下來之后就可以進行本地源碼安裝,直接進入到luasocket目錄進行編譯安裝了 # cd luasocket # make && make install LuaSocket 使用 接下來是LuaSocket擴展的幾種使用方法 1、socket方式請求 -- socket方式請求 local socket = require("socket") local host = "100.42.237.125" local file = "/" local sock = assert(socket.connect(host, 80)) -- 創建一個 TCP 連接,連接到 HTTP 連接的標准 80 端口上 sock:send("GET " .. file .. " HTTP/1.0\r\n\r\n") repeat local chunk, status, partial = sock:receive(1024) -- 以 1K 的字節塊來接收數據,並把接收到字節塊輸出來 -- print(chunk or partial) until status ~= "closed" sock:close() -- 關閉 TCP 連接 2、HTTP訪問請求 -- http訪問請求 http=require("socket.http") result=http.request("http://ip.taobao.com/service/getIpInfo.php?ip=123.189.1.100") print(result) 3、SMTP方法發送mail -- smtp方法發送mail local smtp = require("socket.smtp") from = "<youmail@126.com>" -- 發件人 -- 發送列表 rcpt = { "<youmail@126.com>", "<youmail@qq.com>" } mesgt = { headers = { to = "youmail@gmail.com", -- 收件人 cc = '<youmail@gmail.com>', -- 抄送 subject = "This is Mail Title" }, body = "This is Mail Content." } r, e = smtp.send{ server="smtp.126.com", user="youmail@126.com", password="******", from = from, rcpt = rcpt, source = smtp.message(mesgt) } if not r then print(e) else print("send ok!") end 使用 LuaSocket 還算簡單吧,直接用 require 函數加載進來就行,在例如下面幾個例子 1)輸出一個 LuaSocket 版本信息: local socket = require("socket") print(socket._VERSION) 2)以 socket 的方式訪問獲取百度首頁數據: local socket = require("socket") local host = "www.baidu.com" local file = "/" -- 創建一個 TCP 連接,連接到 HTTP 連接的標准端口 -- 80 端口上 local sock = assert(socket.connect(host, 80)) sock:send("GET " .. file .. " HTTP/1.0\r\n\r\n") repeat -- 以 1K 的字節塊來接收數據,並把接收到字節塊輸出來 local chunk, status, partial = sock:receive(1024) print(chunk or partial) until status ~= "closed" -- 關閉 TCP 連接 sock:close() 3)使用模塊里內置的 http 方法來訪問: local http = require("socket.http") local response = http.request("http://www.baidu.com/") print(response) 一個簡單的 client/server 通信連接 本來想寫成單 server 多 client 的 socket 聊天服務器,不過最后還是卡在客戶端的數據更新上,單進程的 while 輪詢(poll),一個 io.read 就把服務器數據接收給截斷了。 僅靠現有的 LuaSocket 模塊不裝其他第三方模塊,也是很難做一個實時的聊天,雖然有 soket.select 在苦苦支撐,但是這還是一個填不平的坑來了。 可能用上面向並發的 concurrentlua 模塊會解決這個數據接收阻塞問題,這個以后再看看,現階段的成果是:在客戶端的終端上敲一些東西后回車會通過 socket 給服務器發送數據,服務器接收到數據后再返回顯示在客戶端的終端上。 一個簡單的東西,純屬練手,代碼如下: server端 -- server.lua local socket = require("socket") local host = "127.0.0.1" local port = "12345" local server = assert(socket.bind(host, port, 1024)) server:settimeout(0) local client_tab = {} local conn_count = 0 print("Server Start " .. host .. ":" .. port) while 1 do local conn = server:accept() if conn then conn_count = conn_count + 1 client_tab[conn_count] = conn print("A client successfully connect!") end for conn_count, client in pairs(client_tab) do local recvt, sendt, status = socket.select({client}, nil, 1) if #recvt > 0 then local receive, receive_status = client:receive() if receive_status ~= "closed" then if receive then assert(client:send("Client " .. conn_count .. " Send : ")) assert(client:send(receive .. "\n")) print("Receive Client " .. conn_count .. " : ", receive) end else table.remove(client_tab, conn_count) client:close() print("Client " .. conn_count .. " disconnect!") end end end end client端 -- client.lua local socket = require("socket") local host = "127.0.0.1" local port = 12345 local sock = assert(socket.connect(host, port)) sock:settimeout(0) print("Press enter after input something:") local input, recvt, sendt, status while true do input = io.read() if #input > 0 then assert(sock:send(input .. "\n")) end recvt, sendt, status = socket.select({sock}, nil, 1) while #recvt > 0 do local response, receive_status = sock:receive() if receive_status ~= "closed" then if response then print(response) recvt, sendt, status = socket.select({sock}, nil, 1) end else break end end end 轉載:D.H.Q的爛筆頭