socket:provides easy access to TCP, UDP, DNS, SMTP, FTP, HTTP, MIME and much more.
文档
=============
Refer to luasocket documentation
范例
=======
Here is a small TCP Server example
--// script n�1 example START -----------------------------------------------------
package.cpath = "./lib/?.so;./lib/?.dll;./lib/?.dylib;"
require("socket")
local host = "*"
local port = 8080
print("Binding to host '" ..host.. "' and port " ..port.. "...")
local sock, err = socket.tcp()
if not sock then error("Error socket tcp() : "..err) end
sock:setoption("reuseaddr", true)
local res, err = sock:bind(host, port)
if not res then error("Error socket bind() : "..err) end
res, err = sock:listen(5)
if not res then error("Error socket listen() : "..err) end
i, p = sock:getsockname()
assert(i, p)
print("Waiting connection from talker on " .. i .. ":" .. p .. "...")
c = assert(sock:accept())
print("Socket connected")
l, e = c:receive(1)
while not e do
print(l)
l, e = c:receive(1)
end
print("Socket "..e)
--// script n�2 example STOP -----------------------------------------------------
Here is a small TCP Client example
--// script n�2 example STOP -----------------------------------------------------
package.cpath = "./lib/?.so;./lib/?.dll;./lib/?.dylib;"
require("socket")
local tcp = socket.tcp
local sock, err = socket.tcp()
if not sock then
error("Error socket.tcp() : "..err)
else
local res, err = sock:connect("127.0.0.1", 8080)
if not res then
error("Error sock:connect() : "..err)
else
local nb, err = sock:send("hello\r\n")
print(nb.." characters sent")
end
end
--// script n�2 example STOP -----------------------------------------------------
socket的相关信息
version 2.0.2
website http://luaforge.net/projects/luasocket/
author Danilo Tuler, Diego Nehab